admin管理员组

文章数量:1415111

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.

本文标签: sortListing the most popular 8 city (custom fields) as used in posts