0

I have setup my custom post's custom taxonomy's permalink structure to be:

custom post type > custom taxonomy > post (from this helpful Q&A)

Now I'm hoping to be able to code taxonomy.php to automatically loop posts based on the current permalink, ie: if the URL is example.com/writing/fiction/ then the page (taxonomy.php) will show all posts with the taxonomy term "fiction" or if the URL is example.com/writing/non-fiction then taxonomy.php will show all posts with taxonomy term "non-fiction".

The goal is to only need 1 taxonomy.php file.

Here is current code (comment where I'm looking to make term automatic/dynamic):

    <?php 
        $posts = array (
            'post_type' => 'writing',
            'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'type',
                    'terms' => 'fiction', // looking to automate this, based on URL
                ),
            ),
        );
        $loop = new WP_Query( $posts );
        while ( $loop->have_posts() ) : $loop->the_post();
    ?>
            <a href="<?php the_permalink();?>">
                <?php the_title('<h2>','</h2>');?>
            </a>
    <?php endwhile; wp_reset_postdata(); ?>
  • is there a reason you're creating a new query in the template rather than using the default main query? – Milo Aug 22 '18 at 3:05
  • @milo sorry, I'm not sure what you mean? I'm needing to query the custom post type and this is how I've always done that :S – codeview Aug 22 '18 at 3:08
  • WordPress automatically generates the main query based on the requested URL. If you're viewing a taxonomy archive you don't have to create the query yourself, you just need to run the default loop like while ( have_posts() ): to output the contents of the main query. – Milo Aug 22 '18 at 3:12
  • Gonna sound stupid here, but, so...take out $loop = new WP_Query( $posts );? – codeview Aug 22 '18 at 3:24
  • Yes, and any reference to $loop->. If you look at the templates in any of the default themes that come with WordPress, they all contain this basic loop that just outputs main query. You only need to create your own query if you're outputting something separate and different from what would normally be viewed via that URL. – Milo Aug 22 '18 at 3:30
1

Got it to work with:

$queried_object = get_queried_object () ;

and

'terms' => $queried_object->slug,

full code now:

    <?php
        $queried_object = get_queried_object () ;
        $posts = array (
            'post_type' => 'writing',
            'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'type',
                    'field' => 'slug',
                    'terms' => $queried_object->slug,
                ),
            ),
        );
        $loop = new WP_Query( $posts );
        while ( $loop->have_posts() ) : $loop->the_post();
    ?>
            <a href="<?php the_permalink();?>">
                <?php the_title('<h2>','</h2>');?>
            </a>
    <?php endwhile; wp_reset_postdata(); ?>
0

You should be able to get what you're after with:

'terms' => get_query_var( 'type' ),
  • Ah! Very nice. Not sure which method is better/faster/stronger but thank you for this option. – codeview Aug 21 '18 at 23:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.