I have added tags to a custom post type, on its $args array using 'taxonomies' => array('post_tag'). Im using the following code on tag.php to loop posts from a certain tag. It works well but pagination wont work fine. When I try to go to page 2, it gives me a 404 error. If I take out the custom query, it wont loop the custom post type posts. How can I fix it?
<?php
$term = get_queried_object();
$slugTerm = $term->slug;
$nameTerm = $term->name;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_args = array(
'post_type' => 'blog',
'paged' => $paged,
'tax_query' => array(
array (
'taxonomy' => 'post_tag',
'field' => 'name',
'terms' => $nameTerm,
)
),
);
$query = new WP_Query( $query_args );
if($query -> have_posts()):while($query -> have_posts()):$query -> the_post();
?>
POST CONTENT
<?php endwhile; ?>
<div class="pagination">
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(array( 'query' => $query ) ); } ?>
</div>
<?php endif; ?>