Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hi there 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

http://i50.tinypic.com/6f7fww.png

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

share|improve this question
add comment

1 Answer

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.