• Resolved McGee

    (@maniche)


    Hello,

    Im making “upcoming events” web page.
    most of content in post is managed with custom fields.
    (like event date, time, location, city, music genre etc. etc.)
    basicaly, this is what i want to do:

    -there are 6 upcoming events in club “broadway”

    just like we can have top list of categories(by number of posts), i want to have top list of most entered values in some custom field.
    in this case, custom field key is “club” and value wolud be “brodway”, entered 6 times and that makes it on top of my “most active clubs” list.

    is there any way to have lists of most used values in specific custom fields?

    thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • <?php
    // This query will list the top 5 values for a given meta_key
    $mkey = 'x';  // The meta_key to query
    $row_max = 5;  // The maximum rows for that meta_key
    $sql = "SELECT meta_key,meta_value, count(post_id) as counter FROM $wpdb->postmeta
    WHERE meta_key = '$mkey'
    GROUP BY meta_key,meta_value
    ORDER BY meta_key,count(post_id) DESC
    LIMIT 0,$row_max
    ";
    $rows = $wpdb->get_results($sql);
    foreach ($rows as $row) {
      echo "<p>Value: $row->meta_value  Count: $row->counter</p>";
    }
    ?>
    Thread Starter McGee

    (@maniche)

    Thank you very much, sir!

    You are welcome. Now, please use the dropdown at top right to mark this topic ‘Resolved’.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Top list of most used custom field values’ is closed to new replies.