Pagination not working in custom loop WP_Query

Question

Please help with pagination

I have code that works fine (functions.php)

function projects_custom_number_of_posts( $query ) {
    $query->set( 'posts_per_page', 2 );
    return;
}
add_action( 'pre_get_posts', 'projects_custom_number_of_posts');

when I change to, pagination stops working

function projects_custom_number_of_posts( $query ) {
    $postsperpage = $query->get( 'posts_per_page' );
    $query->set( 'posts_per_page', $postsperpage );
    return;
}
add_action( 'pre_get_posts', 'projects_custom_number_of_posts');

It is necessary that the data is taken from the ‘posts_per_page’

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('product_cat' => $cat, 'post_type' => 'product',  'posts_per_page' => 2, 'paged' => $paged ); $query = new WP_Query( $args );

if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
0
Vova 2 years 2020-12-29T08:10:26-05:00 0 Answers 4 views 0

Leave an answer

Browse
Browse