WordPress pagination, wp_query not working on page 2

I have a problem with WP Pagination.

Previously, my pagination worked on taxonomy pages and was displaying same result on page 2 than page 1 on Static Homepage.
I found a solution to this problem, and 3 days ago all my page worked perfectly.

Read More

Until yesterday : now pagination still works on taxonomy pages, but on my static Homepage, page 1 works, but page 2 and more return “No article in this category” like if wp_query loop don’t find results.

No file has been edited, functions are the same than 3 days ago.

PS : If I remove correction of first bug (Page 2 shows the same than page 1), the bug don’t come back, homepage page 2 still returns “No article in this category”;

Here is my code :

    // Define $post_type in previous included file, already checked if parameter correctly retrieved 
    $post_type = array('magicrecipe', 'post', 'yoga-article', 'feelgood', 'gastronomie', 'voyage');

    <ul class="articlesList row mw">

    <?php

    $taxQuery = array('taxonomy' => $taxonomy,
                'field' => 'slug',
                'terms' => $categoryValue);

    // "Page 2 show the same than page 1" fix
    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }


    if ($search) {
        $args = array(
        'posts_per_page' => 12,
        'paged'=>$paged,
        'orderby'=> 'date'
        );
        $args['s'] = $search;
    }else{
        $args = array(
            'post_type' => $post_type, 
            'posts_per_page' => 12,
            'orderby'=> 'date',
            'paged'=> $paged,
            'tax_query' => array(array($taxQuery))
        );
    }

    $loop = new WP_Query($args); 

    $count = $loop->post_count;

    // Pagination fix
    $temp_query = $wp_query;
    $wp_query   = NULL;
    $wp_query   = $loop;

    if ($count == 0) {
        echo '<p class="articles-noArticle">Il n'y a pas encore d'article dans cette catégorie !</p>';
    } else {

        // starting loop
        while ($wp_query->have_posts()) : $wp_query->the_post();
        ?>
        <li class="articlesList articles-article col-1-4 col-m-1-2 col-s-1-1">
            <a href="articles-link">
                <div class="articles-thumbContainer js-squareThumb">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                        <?php the_post_thumbnail(array(500, 500), array('class' => 'articles-thumb')); ?>
                    </a>
                </div>
                <h3 class="article-title"><a href="<?php echo get_post_permalink(); ?>"><?php the_title(); ?></a></h3>
                <p class="article-excerpt"><?php the_excerpt(); ?></p>
                <a href="<?php the_permalink(); ?>" class="article-link">En savoir plus</a>
            </a>
        </li>


    <?php
    endwhile;
}
</ul>
<div class="pagination">
<?php 
    // Custom query loop pagination
    echo paginate_links();
    // wp_reset_query();
?>
</div>

Thanks for your help, I search everywhere but found anything about this problem.

Related posts

1 comment

  1. You have too many conditions.

    <?php
    
        //You don't need that
        /*if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
        elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
        else { $paged = 1; }*/
    
        //Try this:
        <?php
              wp_reset_query();
              $paged = get_query_var('paged', 1);
    
               $args = array(
                'post_type' => $post_type,
                'posts_per_page' => '12',
                'paged' => $paged,
                'order' => 'DESC',
                'orderby' => 'post-date'
              );
              $loop = new WP_Query( $args);
              ?>
    

Comments are closed.