dougai2427 2019-07-13 13:20
浏览 285
已采纳

SQL从数据库中获取Woocommerce产品缩略图

I've been searching through the past two days to find out where exactly could the Woocommerce products thumbnails (images) texts and URL's are stored inside the database tables, but still cannot figure this out!

I'm in a situation where I must use SQL queries to move the products data into another tables, and I have to implement the process from my phpmyadmin panel.

I already searched the wp_posts and wp_postmeta tables, wp_posts contains a guide column for the url to the product which post_type like 'product%', So far I know that in general the post stores it's thumbnalis link inside one of the posts with a type of attachment , while I need the posts with a post_type of product or like so.

Hope I can find some answers here, Thanks.

  • 写回答

1条回答 默认 最新

  • dongziduo9762 2019-07-13 13:47
    关注

    You can use the Wordpress WP_Query to get the thumbnail image of product.

    $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();
    

    Solution 2- using the custom query as you mention in query.

    //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>";
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R