Different number of posts on first page in Shop

// Different number of posts on first page in Shop
function tax_and_offset_shop( $query ) {
    if ($query->is_main_query() && !is_admin()) {
        if (is_shop()) {
            $catid = "";
        } else {
            $cate = get_queried_object();
            $catid = $cate->term_id;
            $taxonomy = $cate->taxonomy;
            $taxonomy_name = $cate->name;
            $taxonomy_slug = $cate->slug;
        }  
        $limit_post =  get_theme_mod('products_pr_page');
        $offset = 2;

        if($catid != ""){
            if ($taxonomy == 'pa_brand') {
                if (get_field("custom_mod", "pa_brand_" . $catid)) {
                    if (!$query->is_paged()) {
                        $query->set('posts_per_page', $offset + $limit_post);
                    } else {
                        $offset = $offset + ( ($query->query_vars['paged']-1) * $limit_post );
                        $query->set('posts_per_page', $limit_post);
                        $query->set('offset', $offset);
                    }
                }
            }else{
                if (get_field("custom_mod", "product_cat_" . $catid)) {
                    if (!$query->is_paged()) {
                        $query->set('posts_per_page', $offset + $limit_post);
                    } else {
                        $offset = $offset + ( ($query->query_vars['paged']-1) * $limit_post );
                        $query->set('posts_per_page', $limit_post);
                        $query->set('offset', $offset);
                    }
                }
            }
        }
    }
}
add_action('pre_get_posts', 'tax_and_offset_shop');
    
function shop_offset_pagination( $found_posts, $query ) {
    $offset = 2;
    if($query->is_main_query() && !is_admin()) {
        if (is_shop()) {
            $catid = "";
        } else {
            $cate = get_queried_object();
            $catid = $cate->term_id;
            $taxonomy = $cate->taxonomy;
            $taxonomy_name = $cate->name;
            $taxonomy_slug = $cate->slug;
        }  
        if($catid != ""){
            if ($taxonomy == 'pa_brand') {
                if (get_field("custom_mod", "pa_brand_" . $catid)) {
                    $found_posts = $found_posts - $offset;
                }
            }else{
                if (get_field("custom_mod", "product_cat_" . $catid)) {
                    $found_posts = $found_posts - $offset;
                }
            }
        }
    }
    return $found_posts;
}
add_filter( 'found_posts', 'shop_offset_pagination', 10, 2 );

Leave a Reply

Your email address will not be published. Required fields are marked *