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.

2 Answers
2

All right, I think I see what’s going on…

As per WP pagination parameters documentation, posts_per_page is indeed cached. Somewhat… Basically, you could set this to “10” the first time, as you do, and then to 5 after that, but then use “offset” or “paged” parameters to get the posts you want.

Your “load-next-amount” variable would simply be a counter of which “offset” or “page” you want to access…

Using offset, you could pass “load-next-amount” equals to “0”, “1”, “2”, …

$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' => 10, // <--------
    'offset' => 10 + (intVal($_POST['load-next-amount']) * 5), // <--------
    'suppress_filters' => true 
);
$myquery = new WP_Query( $myArgs );

Hope this helps!

Tags:

Leave a Reply

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