最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sort - Listing the most popular 8 city (custom fields) as used in posts

programmeradmin2浏览0评论

I am creating a theme for a classifieds website and want to list the top 8 popular city which mentioned as posts custom fields. That means the top 8 cities which have the most classifieds. It will looks like this:

I saw some examples of sorting custom fields but couldn't manage to make them work properly.

I am creating a theme for a classifieds website and want to list the top 8 popular city which mentioned as posts custom fields. That means the top 8 cities which have the most classifieds. It will looks like this:

I saw some examples of sorting custom fields but couldn't manage to make them work properly.

Share Improve this question edited Aug 26, 2019 at 8:51 Jan Doggen 16911 bronze badges asked Nov 17, 2012 at 19:31 Emre CaglarEmre Caglar 511 gold badge1 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Let MySQL do the job:

global $wpdb;

$metakey = 'YOUR_METAKEY_GOES_HERE';

$results = $wpdb->get_results(
    $wpdb->prepare(
        "SELECT meta_value, COUNT(*) AS counter from {$wpdb->postmeta} WHERE meta_key = %s GROUP BY meta_value ORDER BY counter DESC", $metakey, ARRAY_N )
    );
);

$resuls is an array with the ordered results.

发布评论

评论列表(0)

  1. 暂无评论