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="https://wordpress.stackexchange.com/questions/277843/<?php echo get_term_link( get_queried_object() ); ?>all/">
Show All
</a>
<?php endif; ?>