2

I have a custom post type called game_go and a taxonomy called game_go_series. Some example terms are chase , compete, victory, etc. I have a file called taxonomy-game_go_series which displays only the posts in paginated form of an individual term.

The second page I want to create needs to show all that individual term's posts. In other words, there would be two pages that have the same term.

Also, and this is important, I know how to do this by using the term in the filename (e.g. taxonomy-game_go-chase.php), but since we'll be adding more terms, it'll become a pain to have to create a template file each time.

Is there a way to do this where pages by term-taxonomy are created automatically when we add new terms?

Let me know if I can provide further clarification. Thanks!

6
  • I would note that this question implies the filename determines the content, when it's actually the other way around, the content is retrieved long before WP knows which template to load. Instead, would it not make more sense to ask about your original problem? AKA "How do I provide a show all posts link in a paginated term archive?", this is a classic XY problem
    – Tom J Nowell
    Aug 23, 2017 at 12:58
  • So to make more clear, the only difference between your first and second archive is that the first displays posts with pagination and the second have to show them all without pagination?
    – ClemC
    Aug 23, 2017 at 12:59
  • @ClemC exactly! I'm not sure how to create two new pages that show the same individual term's post
    – bhood
    Aug 23, 2017 at 13:12
  • 1
    @TomJNowell well put! I updated the main subject line and that's essentially what I'm trying to do.
    – bhood
    Aug 23, 2017 at 13:15
  • The only thing to keep in mind is that if there are a lot of posts to show, then bad things can happen, there's only so much time and memory to load up the posts, and if there are hundreds of thousands it can grind to a halt
    – Tom J Nowell
    Aug 23, 2017 at 13:22

1 Answer 1

1

This method will set it up so that if you add /all to the end of your taxonomy archives, it will show all posts.

First, when registering the taxonomy, make sure you set the ep_mask to EP_CATEGORIES. This means we can add a custom endpoint to it.

function wpse_277843_register_taxonomy() {
    register_taxonomy( 'game_go_series', 'game_go', array(
        'rewrite' => array(
            'slug'    => 'series',
            'ep_mask' => EP_CATEGORIES,
        ),
    ) );
}
add_action( 'init', 'wpse_277843_register_taxonomy' );

Don't copy all that code, just make sure you do the ep_mask thing in your code, because it's probably not already set that way.

To add ep_mask to Custom Post Types UI generated taxonomies, do this:

function wpse_277843_cptui_ep_mask( $args, $taxonomy_slug, $taxonomy_args ) {
    if ( 'game_go_series' == $taxonomy_slug ) {
        $args['rewrite']['ep_mask'] = EP_CATEGORIES;
    }

    return $args;
}
add_filter( 'cptui_pre_register_taxonomy', 'wpse_277843_cptui_ep_mask', 10, 3 );

Then create the all rewrite endpoint to the EP_CATEGORIES mask.

function wpse_277843_all_endpoint() {
    add_rewrite_endpoint( 'all', EP_CATEGORIES );
}
add_action( 'init', 'wpse_277843_all_endpoint' );

This does mean that the endpoint will also be valid for Categories, but we can ignore them for any custom behaviour later. Unfortunately adding endpoints only to a custom taxonomy appears to be impossible right now. It just means that going to /category/category-name/all won't throw a 404. The /all will just be ignored (or you can apply the same behaviour for categories, if you want).

Then, in pre_get_posts, if the all endpoint is accessed on your custom taxonomy, set posts_per_page to -1:

function wpse_277843_all_posts( $query ) {
    if ( $query->is_main_query() && $query->is_tax( 'game_go_series' ) ) {
        if ( isset( $query->query_vars['all'] ) ) {
            $query->set( 'posts_per_page', -1 );
        }
    }
}
add_action( 'pre_get_posts', 'wpse_277843_all_posts' );

Then you can add a link to your template to the /all version, if you're on a taxonomy archive already, and if it's not already the /all version:

<?php if ( is_tax( 'game_go_series' ) && get_query_var( 'all', false ) === false ) : ?>
    <a href="<?php echo get_term_link( get_queried_object() ); ?>all/">
        Show All
    </a>
<?php endif; ?>
5
  • Thanks for all this!! After looking over it, it looks like you definitely understand my problem and your answer all makes sense but do you by any chance know if I can "inject" that ep_mask parameter into my taxonomy which is being generated from the CPT UI Plugin? I could copy the generated code for the taxonomy into my functions.php but want to avoid this as I have tons of other taxonomies generated from the plugin.
    – bhood
    Aug 23, 2017 at 14:35
  • Updated my answer with some code based on wordpress.org/support/topic/… Aug 23, 2017 at 14:41
  • Actually, it looks like ep_masks is being set to an integer. Is that normal?
    – bhood
    Aug 23, 2017 at 15:54
  • EP_CATEGORIES is an integer, so yes that's right, is this not working? Aug 24, 2017 at 6:03
  • Nah unfortunately not...keeps returning a 404 error when I navigate to the /all page.
    – bhood
    Aug 24, 2017 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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