过去两天我一直在寻找 Woocommerce 产品缩略图(图像)文本和 URL 到底存储在数据库表中的位置,但仍然无法弄清楚!
我面临的情况是,我必须使用 SQL 查询将产品数据移动到另一个表中,并且我必须从 phpmyadmin 面板实现该过程。
我已经搜索了 wp_posts 和 wp_postmeta
表,wp_posts
包含产品 url
的引导列,post_type 如下'product%'
,到目前为止,我知道,一般来说,帖子将其缩略图链接存储在具有 attachment
类型的帖子之一中,而我需要带有 的帖子
或类似这样。 product
的 post_type
希望我能在这里找到一些答案,谢谢。
最佳答案
您可以使用Wordpress WP_Query来获取产品的缩略图。
$args = array(
'post_type' => 'product', //post type of product
'posts_per_page' => -1
);
$query= new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
global $product;
//woocommerce_get_product_thumbnail is use to get the product thumbnail link
echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
endwhile;
wp_reset_query();
解决方案 2- 使用您在查询中提到的自定义查询。
//Using custom query get the details from wp_postmeta and wp_posts
//_wp_attached_file - meta key for image
$querystr = "SELECT p.*, pm2.meta_value as product_image FROM wp_posts p LEFT JOIN
wp_postmeta pm ON (
pm.post_id = p.id
AND pm.meta_value IS NOT NULL
AND pm.meta_key = '_thumbnail_id'
)
LEFT JOIN wp_postmeta pm2 ON (pm.meta_value = pm2.post_id AND pm2.meta_key = '_wp_attached_file'
AND pm2.meta_value IS NOT NULL) WHERE p.post_status='publish' AND p.post_type='product'
ORDER BY p.post_date DESC";
$upload_dir = wp_upload_dir();
//get wp_upload url
$wp_upload_url = $upload_dir['baseurl'];
$pageposts = $wpdb->get_results($querystr, OBJECT);
foreach ($pageposts as $post){
echo "<br /><a href='".$post->guid."'><img src='". $wp_upload_url.'/'.$post->product_image."'>".$post->post_title."</a>";
}
关于php - SQL 从数据库获取 Woocommerce 产品缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57019560/