WordPress查询商品销量前10名排行榜
文章描述:
WordPress自定义查询语句,查询销售最好的10个产品,并且进行排序功能。
载入查询类
<?php
global $wpdb;
$wpdb->show_errors();
查询条件
$post_status = 'publish';
$post_type = 'product';
查询语句
$items = $wpdb->get_results("SELECT ID,post_title,(select count(*) from wp_wc_order_product_lookup where product_id = c.ID ) as count FROM ".$wpdb->prefix."posts AS c WHERE post_status='$post_status' and post_type='$post_type' ORDER BY count DESC LIMIT 0,10 ",ARRAY_A);
浏览量查询
$wpdb->get_results("SELECT a.ID,a.post_title,a.post_date,b.meta_value FROM ".$wpdb->prefix."posts as a LEFT JOIN ".$wpdb->prefix."postmeta as b ON a.ID = b.post_id WHERE a.post_status='$post_status' AND a.post_type='$post_type' AND b.meta_key='views' ORDER BY b.meta_value DESC LIMIT 0,10",ARRAY_A);
发布时间:2023/04/07
发表评论