If all you’re trying to do is to display all post types on the taxonomy archive, then you can easily accomplish that by filtering the query at pre_get_posts
. Assuming a taxonomy foobar
:
function wpse111033_filter_pre_get_posts( $query ) {
if ( $query->is_main_query() && $query->is_tax( 'foobar' ) ) {
$query->set( 'post_type', 'any' );
}
}
add_action( 'pre_get_posts', 'wpse111033_filter_pre_get_posts' );
This callback says “if I’m on the foobar
taxonomy archive, and I’m the main query, then query all post types, rather than only the post
post-type.”
Edit
Based on this comment:
Say there’s Post Type X and Post Type Y. /taxonomy_name/term1 would list Term 1’s posts, and posts in Post Type X and Post Type Y (in the main content area under the posts) that include that term.
For this, you will either need to create a custom taxonomy.php
, that does what you’re trying to accomplish, or else create a custom page template.
Now say you’re on /taxonomy_name/term1/page/2 I don’t want the 2 post type loops to run and I want the taxonomy page to work as normal.
This will be tricky, but entirely possible. The process will look something like this:
- Create a custom
taxonomy.php
- Use the
pre_get_posts
callback to include all post types - Use the
$paged
oris_paged()
conditional to determine whether you’re on the first page or a “paged” page - If
! is_paged()
, then you’re on the first page, so display a list of posts - if
is_paged()
, then you’re on a page other than the first page, so offset the posts, and display a normal loop
taxonomy.php
/**
* Simplified markup for taxonomy archive index
*/
get_header();
// First page
if ( ! is_paged() ) {
?>
<ul>
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<li><a href="https://wordpress.stackexchange.com/questions/111033/<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile; endif;
?>
</ul>
<?php
}
// Subsequent pages
else {
if ( have_posts() ) : while ( have_posts() ) : the_post();
// Normal loop output here
endwhile; endif;
}
get_footer();
pre_get_posts
function wpse111033_filter_pre_get_posts( $query ) {
if ( $query->is_main_query() && $query->is_tax( 'foobar' ) ) {
// Include all post types
$query->set( 'post_type', 'any' );
// On the first page,
// display all posts
if ( ! $query-is_paged() ) {
$query->set( 'posts_per_page', '-1' );
}
// On subsequent pages,
// offset posts
else {
// You could do: $query->set( 'offset', '-10' );
// But this will break pagination,
// so we need to fix it again
$query->set( 'posts_per_page', '10' );
$page_offset = ( ( $query->query_vars['paged']-1 ) * 10 ) - 10;
$query->set( 'offset', $page_offset );
}
}
}
add_action( 'pre_get_posts', 'wpse111033_filter_pre_get_posts' );
found_posts
But messing with the page offset breaks the query pagination (because WordPress won’t take it into account), so we need another fix:
function wpse111033_filter_found_posts( $found_posts, $query ) {
if ( $query->is_main_query() && $query->is_tax( 'foobar' ) ) {
if ( $query->is_paged() ) {
// Get the right posts
return $found_posts + 10;
}
}
}
add_filter( 'found_posts', 'wpse111033_filter_found_posts', 10, 2 );