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();
posts_per_page
to the currentposts_per_page
? You're also doing it for every query that gets run.pre_get_posts
filter is pointless because you then replace the main query and use a secondaryWP_Query
in the template. Usepre_get_posts
and onlypre_get_posts
and the problem will vanish. Your second code block is also a non-operation