Output the content of a term organised by each of the CPT?

I have four CPT and several shared Custom Taxonomies for all.

I need to retrieve and output the content of a term (let’s say “environmental-crisis” of the shared taxonomy “Crisis Type”, and organise the output by each of the CPT, so the final result hopefully looks like this (or something alike):

Custom Post Type – NGOs:
– post under NGO CPT with taxonomy term “environmental-crisis”
– post under NGO CPT with taxonomy term “environmental-crisis”

Custom Post Type – Projects:
– post under Projects CPT with taxonomy term “environmental-crisis”
– post under Projects CPT with taxonomy term “environmental-crisis”

Custom Post Type – Events:
– post under Events CPT with taxonomy term “environmental-crisis”
– post under Events CPT with taxonomy term “environmental-crisis”

..and so on. You get the idea 😉

Given that taxonomy-taxonomy-term.php (taxonomy-crisis-environmental-crisis.php) does not do the trick and that would require to create way too many templates files (one for each crisis?) with tons of new instances of WP_Query, how on Earth can I feed into the loop the right information?

I’ve read about pre_get_posts and also endpoints, but those concepts are over my head.

I’ve also have defined the custom taxonomies before defining the CPTs as I have read that affects the ability of a CPT to use the taxonomy rewrite. Furthermore, seems that after WordPress 4,7 endpoints have been removed. Any clarification on this points would be supper too.

Finally, I’m using the global variable $post to retrieve the slug of the current page, but I have to stress that I’m scared as I’ve read that this might result in problems as “any custom query or custom code can change the value of $post.

So far my code lacks the ability to output all CPT separately – they are output by date, all together:

add_filter(‘pre_get_posts’,’sorting_regions’);

function sorting_regions( $query ) {

if ( $query->is_tax( ‘crisis’ ) && $query->is_main_query() ) {
$query->set( ‘post_type’, array( ‘ngos’, ‘papers’, ‘events’ ) );
$query->set(‘meta_query’, array(
array(
‘taxonomy’ => ‘crisis’,
‘field’ => ‘slug’,
‘terms’ => $queried_object->slug,
)
) );
}

return $query;
}

I’m guessing I need something like foreach ($taxonomies as $taxonomy) : …

Any help would be much appreciated!

Read more here:: Output the content of a term organised by each of the CPT?

Leave a Reply

Your email address will not be published. Required fields are marked *