You’re overwriting the main query with a new query where you don’t specify any tag parameters, which is why you’re seeing all posts and not those specific to the tag you’re viewing. The answer-
Do not modify the main query in the template.
There’s almost never a legitimate reason to have to modify the main query this way. Remove the 3 lines starting from $paged =
and ending with $wp_query = new WP_Query($args);
.
Use the pre_get_posts
action to modify any query parameters before the main query is run.
Put the following code in your theme’s functions.php
:
function wpa101549_tag_query( $query ) {
if ( $query->is_tag() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 20 );
}
}
add_action( 'pre_get_posts', 'wpa101549_tag_query' );
This will run before the database is queried, if a tag page is being viewed, and it is the main query.
Additionally, you’ll have to correct your pagination function call. This:
pagination($additional_loop->max_num_pages);
is calling the pagination
function and passing $additional_loop->max_num_pages
, but there is no $additional_loop
defined anywhere in your template. It should be:
pagination($wp_query->max_num_pages);