I’m loading a list of products on an archive type page and creating a custom query to do so. I want to create a ‘load more’ functionality so that clicking the ‘load more’ button at the bottom of list loads the next 10 products.
I thought it would be as easy as using a variable to set ‘posts_per_page’ and simply increment it by 10 when the button is clicked. I get strange results however. This code is not the final code but just to illustrate this problem.
if(isset( $_POST['load-next-amount'] )){
$pageamount = 10
}
else {
$pageamount = 5;
}
So the first time this is loaded it resolves to false because the button hasny’t yet been clicked, so posts_per_page will be set to 5. Then when clicking the button, you would expect 10 to load but for some reason it continues to load 5. If however I change the code to this:
if(isset( $_POST['load-next-amount'] )){
$pageamount = 4
}
else {
$pageamount = 5;
}
it will load 4. So I cannot increase the amount of posts_per_page through this variable, but I can decrease it. Strange.
Here is my query:
$myArgs = array(
'category' => '',
'category_name' => '',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'include' => '',
'exclude' => '',
'meta_key' => '_price_per_1hr_lesson',
'meta_value' => '',
'post_type' => 'job_listing',
'post_mime_type' => '',
'post_parent' => '',
'post__in' => $what,
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'job_listing_region' => "'" . $city . "'",
'post_status' => 'publish',
'posts_per_page' => $pageamount,
'suppress_filters' => true
);
$myquery = new WP_Query( $myArgs );
UPDATE: using print_r I can see that the correct value is being assigned to the variable and that value is being correctly assigned to ‘posts_per_page’. For whatever reason, it ignores it UNLESS it is LESS than the previously assigned value. For instance if I set the else statement to $pageamount = 20, the first time the page loads there will be 20, then if i click the button(sending the variable to the script which is 10) it will load 10. It will load any number of posts I hard code as long as it’s less than 20.
Obviously there is something in WP that is setting a default or something. Still a bit of a newb to WP so I don’t know. Looking at wp_config and I dont’ see anything.