0

I use this code in tag.php:

 global $wp_query;
     $paged = (get_query_var('paged')) ? get_query_var('page') : 1;
     $termsss = $wp_query->queried_object;
     $my_argsss = array (
        'post_type' => array('pavilion','post','catalog','tenders','services'),
        'tag' => $termsss->slug,
        'posts_per_page' =>'20',
        'paged' => $paged
     );

    $wp_query = new WP_Query();
    $wp_query -> query ($my_argsss);

     if ( $wp_query->have_posts() ) {
     while ( $wp_query->have_posts() ) { $wp_query->the_post(); 

     // your code!

     }
}

At first page, we can see every we want, but in another page returns 404 not found!
Any idea?

1

Never re-do the main query. Hook into pre_get_posts and change it. The following needs to go in your functions.php (putting in the template will mean it runs too late):

add_action( 'pre_get_posts', function ( $wp_query ) {
    if ( $wp_query->is_main_query() && $wp_query->is_tag() ) {
        $wp_query->set( 'posts_per_page', 20 );
        $wp_query->set( 'post_type', [
            'pavilion',
            'post',
            'catalog',
            'tenders',
            'services',
        ]);
    }
});

And now your template code should just be:

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post(); 

        // your code!
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.