Forums

WPQuery - Order by category then alphabetical (2 posts)

  1. rusticated
    Member
    Posted 11 months ago #

    I'm trying to use WP_Query to create a list of posts, where the list is sorted first by category, then in alphabetical order. I also want the category title to show up above the first post of that category. This would basically create a list of posts similar to:

    CATEGORY ONE TITLE
    Post A
    Post B
    Post C
    CATEGORY TWO TITLE
    Post D
    Post E
    Post F
    CATEGORY THREE TITLE
    Post A
    Post B
    Post C

    And so on. I can get the posts to sort themselves by category using:

    $second_query = new WP_Query( 'post_type=page&posts_per_page=-1&orderby=meta_value&meta_key=Category&order=asc&post_parent='.$parent );
    
    while ($second_query->have_posts()) : $second_query->the_post();
    
    ?>
    CODE TO DISPLAY POST
    <?php
    
    endwhile;
    
    wp_reset_postdata();

    but can't figure out how to then sub-sort those results alphabetically (they seem to sort themselves by date), or display the category title above the first post of that category.

    Can anyone help me out with this? :)

  2. Peter vanDoorn
    Member
    Posted 11 months ago #

    According to the Codex, orderby can have more than one value, so in theory your ...&orderby=meta_value&... can be ...&orderby=meta_value,title&...

    Hope that helps

    Peter

Reply

You must log in to post.

About this Topic