2

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!

UPDATE: I've found this entry. Seems very clear what I needed to do. The downfall is that the permalink structure will not specify from which CPT we're retrieving the info, as a rewrite rule when registering the taxonomy wont work as I'm guessing a dynamic post type can not be perpended as %post_type%/taxonomy, with_front => false, right?.

I've created a taxonomy-taxonomy.php file with the following code:

<h2 class="headline headline--medium">NGOs</h2>
  <?php
  $args = array(
          'post_type'   => 'ngos',
          'post_status' => 'publish',
          'tax_query'   => array(
            array(
                'taxonomy' => 'crisis',
              'terms' => get_queried_object_id(),
              'field' => 'id'
            )
          )
         );

        $relatedNgos = new WP_Query( $args );
        if( $relatedNgos->have_posts() ) :
        ?>
          <ul>
            <?php
              while( $relatedNgos->have_posts() ) :
                $relatedNgos->the_post();
                ?>
                  <li><?php printf( '%1$s', get_the_title() );  ?></li>
                <?php
              endwhile;
              wp_reset_postdata();
            ?>
          </ul>
        <?php
        else :
          esc_html_e( 'No related NGOs found', '' );
        endif;
        ?>

Creating as many queries as Custom Post Types, so the output is organised.

  • Just so we're clear, you want to display all of Post Type A in Category A then display all of Post Type B in Category A? If so, that's gonna be pretty rough to pull off without modifying the SQL. – Howdy_McGee Aug 2 '18 at 17:21
  • Thanks @Howdy_McGee. Perhaps not all, thus post_per_page would be put into use, but yes: some posts of Post Type A in taxonomy A, then some posts of Post Type B in taxonomy A, and so on... So far the code above does the trick, but not sure if this is even sustainable... – Nils Aug 4 '18 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.