I have the following Custom Post Types which are all linked to the destination
taxonomy.
- Deals
- Accommodation
- Attractions
- Articles
How would I change the taxonomy-destination.php
file to show matching posts from all 4 post types.
For instance, when currently viewing the destination "los angeles"
, it shows all this content together by date rather than separating in sepearte sections.
I would prefer to have them split, deals at the top, followed by the other content not all mixed together.
How would be best to achieve this?
Hook into
pre_get_posts
, check for the taxonomy, and set all four post types as thepost_type
parameter of the query object. Check out the Type Parameters section on theWP_Query
docs for more information.The above could go in your
functions.php
file, or, more appropriately, a plugin.Edit to address the group by requirement:
That’s not going to split them up, however. There a few options to do that. First, you could just leave the main query alone (don’t do the above), and create multiple instances of
WP_Query
and do multiple loops. Not bad.You could also hook into
posts_orderby
and alter the SQL to order the posts by post type as well as whatever else is there:You could even be very granular and order by specific post type with the help of the
FIELD
function.Then just check when the post type changes, and display whatever header you like: