How can I make a list of categories with 4 last posts in each where every category is sorted by meta_value.

For example, template without sorting by meta_value is http://pastebin.com/AeH6vx9b It displays parent categories only.

Few days I am searching something related to categories extra fields but can't find something workable.

This is my best search result. What I have to do with this? I don't understand it. Is it possible to do what I want?

2 Answers 2


This is no solution, just an explanation: WordPress currently doesn't have taxonomy meta data. You could add a stand alone table to add meta data there. Anyway, it's not recommended to add your taxonomy/category/tag meta data to the options table as this one wasn't made to get JOINed to do meta queries by it. My personal recommendation would be that you simply add another taxonomy that has your meta value and as well link this one. This would allow you to do default a tax_query by two taxonomies and their terms.

  • Kaiser thank you, it's a good recomendation. But I don't know how to make another taxonomy with meta. I know how to add another taxonomy, but it wouldn't have meta fields. And the same, I don't know how to link categories with custom taxonomy. Can you help me, please. Any example will be greate
    – okonik
    Mar 8, 2013 at 9:47
  • @okonik You A) ain't got meta data for any taxonomy (or category or post tag). Period. That's a fact that you will have to life with. B) Create an additional taxonomy and when you add Term A / Taxonomy A, then just add meta to it, with adding Term B, C, X / Taxonomy B. And those terms are your taxonomy meta data. It is kind of a "manual" handling for taxonomies, but it is (for queries and other WP internals) the best thing you can do.
    – kaiser
    Mar 8, 2013 at 12:05

Replace your_meta_key_here with the actual meta key you want to sort by. This code will retrieve categories, then for each category, it will fetch the 10 most recent posts belonging to that category, sorted by the specified meta value

$args = array(
    'type'                     => 'post',
    'child_of'                 => 0,
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 1,
    'taxonomy'                 => 'category',
    'pad_counts'               => false,

$categories = get_categories($args);

foreach ($categories as $category) {
    $args = array(
        'posts_per_page' => 10,
        'category' => $category->term_id,
        'orderby' => 'meta_value', // Sort by meta value
        'meta_key' => 'your_meta_key_here', // Replace with your actual meta key

    $query = new WP_Query($args);

    if ($query->have_posts()) {
        echo '<h2>' . $category->name . '</h2>';
        echo '<ul>';

        while ($query->have_posts()) {
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';

        echo '</ul>';


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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