Support » Fixing WordPress » Group Posts by Term on Post Type Archive

  • Resolved Mark Wilkinson


    I have been working on a solution for this for a while and yet to some up with anything that is really good and does not involve multiple queries. Let me outline the background.

    I have a Custom Post Type named People (wpmark_person). Essentially this is the for Meet the Team part of a website. The people are group by a custom taxonomy into different people types. My custom taxonomy is called person type (wpmark_person_type) and it has terms such as:

    • Senior Leaders
    • Instructors
    • Office and Admin

    I want to the use the post type archive for the person post type to output all of the person posts. To do this I have used pre_get_posts in order to set posts_per_page to -1. So I now have all the people posts listing on that archive.

    What I want to do now is to have them sorted (ordered) by person type. Therefore the page would output something like this:

    Senior Leaders
    – Person 1
    – Person 2
    – Person 3
    – Person 4
    Office and Admin
    – Person 5
    – Person 6

    However I cannot find a good way to sort the query. Therefore I would be left with loop through the posts and adding them to arrays depending on the person type. Then loop through those again in order to output the content for each one.

    Any help would be greatly appreciated. Many thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • instead of building people than filtering into types, could you instead just loop types then query people within:

    $types = get_terms('wpmark_person_type',...)
    foreach ( $types as $type) {
        echo $type->name;
        $pplz = get_posts(array(
        'post_type' => 'wpmark_person',
    	//...orderby your preferred method
    	'tax_query' => array(array(
                      'taxonomy' => 'wpmark_person_type',
                      'field' => 'slug',
                      'terms' => $type->slug
        foreach ($pplz as $person)
            echo " - ".$person->post_title;

    Thanks the idea here and In theory yes that will work, but that is not really what I want to do. You see WordPress by default gives me the posts that I want – all the people. However I just want to order them differently.

    I want, if possible although I am thinking that this is not possible at the moment, to have no more additional queries in the template such as get_posts or WP_Query as this querying what I already have.

    Ah I see. Good luck with that! I often find the default/archive pages / WP_Query pretty useless when it comes to organizing by anything other than themselves.. so much smoother to do work-arounds or your own loops. /2cents

    could you add groupby in the query to group type, then on the output keep track of the person type, if different (next group), print the type title?

    Just to follow up I have blogged about a solution for this here:

    Sort Posts by Term on Custom Post Type Archives

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Group Posts by Term on Post Type Archive’ is closed to new replies.