You shouldn’t be doing this:
$searchProductsByCategory = [
'posts_per_page' => 1,
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'orderby' => 'id',
'tax_query' => [
[
'taxonomy' => 'product_category',
'terms' => get_queried_object_id()
]
]
];
$foundProducts = new WP_Query($searchProductsByCategory);
The whole point of taxonomy-product_category.php
is that posts for the current category have already been queried. This is why get_queried_object_id()
is already the correct category, and why queried
is in past tense.
When you want to display the posts for the current category/post type/archive inside the relevant template from the template hierarchy you need to use the main loop:
if ( have_posts() ) :
while ( have_posts() ) : the_post();
endwhile;
endif;
Whenever you’re viewing the latest posts, a single post, a single page, posts in a taxonomy term, search results or a date archive, WordPress automatically queries the posts and you display them with the main loop.
This is the only reason the template hierarchy works. How could a custom taxonomy fall back to index.php
when taxonomy-$taxonomy.php
doesn’t exist if they required different queries? It’s because they aren’t separate queries, they’re the main query.