0

I have a custom loop that shows products according to a given criteria, e.g Products from a particular state, category or general product name search. This is in a page on my website.

The problem is that when I try to navigate the posts, the page counter resets to 3 pages and 22 posts, whereas results were showing 4 pages ( $loops->max_num_pages ) and 31 posts ( $loops->found_posts ) These results in the pagination aren't related to the query. When I manually input /page/4 in the url, I get 0 results

Here's the code

<?php if ( is_coupon_page() || isset($_REQUEST['pagination_base']) && strpos($_REQUEST['pagination_base'],'/coupon/') !== false ) { 
    
  if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
    } elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
    } else {
       $paged = 1;
    }

if( isset( $_REQUEST['search_product'] ) && !empty( $_REQUEST['search_product'] ) ) {

        $find = sanitize_text_field( $_REQUEST['search_product'] );
        $q = 's';
            $loops = new WP_Query( array(
            'post_type' => 'product',
            'paged'   => $paged,
            'posts_per_page' => 12,
            's'              => isset($find) ? $find : '',
            ),);
            
    } elseif(isset( $_REQUEST['cpn_category'] ) && !empty($_REQUEST['cpn_category'] ) || isset( $_REQUEST['wcfmmp_store_category'] ) && !empty($_REQUEST['wcfmmp_store_category'] ) || (isset( $_REQUEST['state'] ) && !empty($_REQUEST['state'] ) ) ) {
    
if( !empty($_REQUEST['cpn_category'] ) ){
    $cat =  sanitize_text_field( $_REQUEST['cpn_category'] );
}else if( !empty($_REQUEST['wcfmmp_store_category'] ) ){
    $cat = sanitize_text_field( $_REQUEST['wcfmmp_store_category'] );
}
$state = sanitize_text_field( $_REQUEST['state'] );

if( !empty ( $_REQUEST['state'] ) && $cat == '' ) {
    $loops = new WP_Query( array(
            'post_type' => 'product',
            'author__in' => $authors,
            'paged'   => $paged,
            'posts_per_page' => 12,
    ),);
} else { 

$loops =new WP_Query( array(
            'post_type' => 'product',
            'paged'   => $paged,
           'posts_per_page' => 10,
        'tax_query'      => array( 
    'relation' => 'AND',
   array(
        'taxonomy' => 'product_cat',
        'field'    => 'term_id',
        'terms'    => array( $cat ), 
        'operator' => 'IN' 
    ), 
   ), 
    ),);
}
    
} else {
    $loops = new WP_Query( array(
            'post_type' => 'product',
            'author__in' => $authors,
            'paged'   => $paged,
            'posts_per_page' => 10,
            ),
            );
}
        ?>
        
         <?php  woocommerce_product_loop_start(); ?>
         
            <?php if ( $loops ): ?>
                  <?php while($loops->have_posts() ): 
                     
         ?>
                        <div class="col span_1_of_4">
                             <span class="hide"><?php var_dump( $_SESSION['args']); ?></span>
                     <?php   $post_object =  get_post_field( 'post_author', get_the_ID() ); ?> 
        <?php  $vendor_lat = get_user_meta( intval($post_object), '_wcfm_store_lat',true ); ?>
        <?php  $vendor_lon = get_user_meta( intval($post_object), '_wcfm_store_lng',true ); ?>
            
            <?php $loops->the_post(); ?>
            <?php   wc_get_template_part( 'content', 'product' );  ?>

                </div> 
        
        
       <?php  endwhile; ?>
               
     <?php woocommerce_product_loop_end(); ?>
     
     <?php $max = ceil ( $loops->found_posts / 10 );
    echo $loops->max_num_pages .'&nbsp;pages&nbsp;and';
    echo $loops->found_posts .'&nbsp;posts';
    ?>
         <?php if ( $loops->max_num_pages > 1 ) {  if(!isset($_SESSION['pg']) ) $_SESSION['pg'] = $loops->max_num_pages; ?>
                <div class="wcfmmp-pagination clearfix">
                     <?php
            $pagination_args = array(
                'current'   => intval( get_url_var('page') ),
                'total'     => $max,
                'base'      => $pagination_base,
                'type'      => 'list',
                'prev_text' => __( '&laquo;', 'wc-multivendor-marketplace' ),
                'next_text' => ( intval( get_url_var('page') )== $max ) ? false :  __( '&raquo;', 'wc-multivendor-marketplace' ),
            );
            $page_links = paginate_links( apply_filters(
        'wcfm_store_list_pagination_args', $pagination_args  ) );
        
            if ( $page_links ) { ?>
                
                    <nav style="padding-bottom: 0;" class="paginations woocommerce-pagination">
                  <?php echo $page_links; ?>
                </nav>
                <?php
            }
            ?>
        </div>
       <?php } wp_reset_query(); ?>
        <?php   else: ?>
                    <p class="wcfmmp-error" style="max-width:100%;"><?php _e( 'No products found!', 'wc-multivendor-marketplace' ); ?></p>
               <?php endif; ?>
     
       
<div class="wcfm-clearfix"></div>

<?php }

I'm also using WP_Query to retrieve products based on location in woo-commerce product page and the pagination has no issue. Please help me, been at it since morning. Thank you

Your Answer

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

Browse other questions tagged or ask your own question.