0

I'm having custom page template that I use as front page of my theme. In that page i have section which lists woocommerce products using custom loop. I want to insert next posts pagination to that section. I'm using next_posts_link() function to generate next page url link. It generates this perfectly, but on my home page page/2 isn't displaying new products but old ones.

Here is my code:

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

    $args = array(
       'post_type'      => 'product',
       'posts_per_page' => $top_selling_products_nr,
       'meta_key'       => 'total_sales',
       'paged'          => $paged,
       'orderby'        => 'meta_value_num'
    );

    <?php if ( $woo_home_query->have_posts() ) : ?>

        <div class="products container grid-wrapper clear">

            <div class="row">
            <?php

                while ( $woo_home_query->have_posts() ) : $woo_home_query->the_post();
                    wc_get_template_part( 'content', 'product' );
                endwhile;

            ?>
            </div>

            <?php if ( $woo_home_query->max_num_pages > 1 ) : ?>

                <nav class="navigation is posts-navigation" role="navigation">
                    <h2 class="screen-reader-text"><?php esc_html_e( 'Posts navigation', 'goodz' ); ?></h2>
                    <div id="infinite-handle" class="nav-links">
                       <div class="nav-previous"><?php next_posts_link( esc_html__( 'Load More', 'goodz' ), $woo_home_query->max_num_pages ); ?></div>
                    </div> <!-- .nav-links -->
                </nav> <!-- .navigation -->

            <?php endif; ?>

        </div>

    <?php endif; ?>

1 Answer 1

-2

Can you please try below code?

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

    $woo_home_query = array(
       'post_type'      => 'product',
       'posts_per_page' => $top_selling_products_nr,
       'meta_key'       => 'total_sales',
       'paged'          => $paged,
       'orderby'        => 'meta_value_num'
    );

    <?php if ( $woo_home_query->have_posts() ) : ?>

        <div class="products container grid-wrapper clear">

            <div class="row">
            <?php

                while ( $woo_home_query->have_posts() ) : $woo_home_query->the_post();
                    wc_get_template_part( 'content', 'product' );
                endwhile;

            ?>
            </div>

            <?php 
            $total_pages = $woo_home_query->max_num_pages;

            if ($total_pages > 1){

                $current_page = max(1, get_query_var('paged'));

                echo paginate_links(array(
                    'base' => get_pagenum_link(1) . '%_%',
                    'format' => '/page/%#%',
                    'current' => $current_page,
                    'total' => $total_pages,
                    'prev_text'    => __('« prev'),
                    'next_text'    => __('next »'),
                ));
            }
            ?>  

        </div>

    <?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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