Overall CODE issues:
Your CODE is so wrong in so many ways that I shouldn’t even attempt to address them here. I suggest you study Official WordPress Theme Handbook properly before doing these sort of customizations.
For the sake of the context, I’m ignoring other issues within your CODE and touching only the issues below:
1. Why index.php
template is being loaded?
First of all, you are getting wrong pagination from your erroneous CODE (check the explanation below). So apart from the first tag page, all the other tag pages are nonexistent (i.e. you need to add more posts to the tag to get more tag pages).
Secondly, According to WordPress Template loading rules, 404.php
template should be loaded for those non-existing tag pages (as you probably know, in HTTP, 404 is the Page Not Found Error CODE). However, since you don’t have a 404.php
template in your theme, index.php
template is being loaded, as it is the ultimate fallback template in a WordPress theme.
Now, if you create a 404.php
template, then you’ll see that it’ll be loaded instead of index.php
for those non-existing tag pages.
Study WordPress Template Hierarchy properly for a better understanding of how different templates are loaded for different content.
2. Fixing the wrong pagination:
As I said above, you’re getting nonexistent tag page numbers within your pagination. To remove those nonexistent page numbers from the pagination, you need to delete the
'total' => $post_query->max_num_pages
line from the paginate_links
function call in tag.php
template file.
The reason is:
-
$post_query
is your custom query, not the main query that should determine the number of pages the tag archive has. You even usedwp_reset_postdata()
beforepaginate_links
function call, so no reason to use that variable here. -
The
total
parameter of thepaginate_links
function by default gets the value of the main WP_Query’smax_num_pages
property. So deleting it will automatically provide the correct value for the pagination from the main WP_Query object.
3. Pagination without new WP_Query
:
In the comments you said:
The reason why I did this is because I didn’t know how to generate the type of pagination I needed (
<Prev 1 2 3 Next>
style) viapaginate_links()
, with the main query.
Well, you don’t need a completely new WP_Query
just for a different pagination style. In fact, the paginate_links()
function don’t need the main WP_Query
object at all!
So all you need for your desired pagination style is:
echo paginate_links( array(
'end_size' => 2,
'mid_size' => 1,
'prev_text' => __( '<< Previous page', 'text-domain' ),
'next_text' => __( 'Next page >>', 'text-domain' )
) );
All the other values are being collected by default (including the main WP_Query
object)! So you can remove your new WP_Query
object in the tag.php
template entirely. Check the paginate_links
documentation.
Having said that, there can be only one more thing you may want (judging from your comments):
4. Control the number of posts per tag page:
You don’t need a new WP_Query
for this either. This can be easily achieved from the following CODE in functions.php
:
function tag_post_per_page_filter( $query ) {
if ( $query->is_main_query() ) {
if ( $query->is_tag ) {
$query->set( 'posts_per_page', 3 );
}
}
}
add_action('pre_get_posts','tag_post_per_page_filter');
Basically it sets the posts_per_page
to 3
in the main WP_Query
object for the tag pages. That’s all.