Creating an archive of a CPT listed by category (or other taxonomy)

I'd like to have an archive of a CPT grouped by the terms of a specific taxonomy associated with that CPT (and only that CPT).

For example, let's say I have a CPT called "Portfolio" and it has a taxonomy called "Subject." Let's say Subject has three terms: Cats, Dogs, and Horses.

The Portfolio archive page would have:
"Cats" and then show the Cats Portfolio items,
"Dogs" and then show the Dogs Portfolio items,
"Horses" and then show the Horses Portfolio items.

The CPT and taxonomies are coded by hand (no plugins). I'm not sure how to set up the query and loop to set up the archive page as I've described.

Thanks for you help!

Laura

  • Bojan Radonic

    Hey there Laura,

    In order to do this you'll have to make changes to the archive template of your custom post type. The easiest way for you to do this would be to use this site:

    http://generatewp.com/wp_query/

    It allows you to generate query with inserting post type, category ID or name. When done simply replace it in your template.

    Please let me know if this helps :slight_smile:

    Best regards,
    Bojan

  • Laura

    Hi, Bojan --

    Thanks for getting back to me. I don't think the link you gave me will accomplish what I want.

    I'd like the CPT archive to collect all the terms in a particular taxonomy and then, for each term, list the name of the term and then the posts that belong to that term.

    As far as I can tell, it's not a simple WP_Query specification because I need to loop through subgroups of different terms.

    I can get the list of terms of a category using something like $categories = get_terms( 'category', 'orderby=count&hide_empty=0' ).

    The tricky part is listing the CPTs by subgroup. I need to be able to loop through each category and list the posts that belong to each one.

    If that generator will allow the subgrouping, would you please have it generate an example and paste it here?

    Again, the result I want is something like:

    Cats
    [cpt with cats in it -- 1]
    [cpt with cats in it -- 2]

    Dogs
    [cpt with dogs in it -- 1]
    [cpt with dogs in it -- 2]
    [cpt with dogs in it -- 3]

    Horses
    [cpt with horses in it -- 1]

    Maybe my question still isn't clear. If so, please ask. I'd really like help with this!

    That generator is cool, though. It's a great thing for me to know about!

    Thanks,

    Laura

  • Predrag Dubajic

    Hi @Laura,

    My apologies for long overdue on this thread.

    Unfortunately I can't say that I fully understand what do you want to accomplish.

    Do you want to show all CPT posts in your portfolio page but organize them so that first few are cats, then dogs and then horses?

    May I suggest looking at this thread to see if some of the solution work for your issue:
    http://wpquestions.com/question/showChrono/id/9371

    Have you considered using multiple loops on page to accomplish this?

    Best regards,
    Predrag

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.