2

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.

  • How many posts fulfill your search? Is it possible you don't have more than 5? The problem I see here is you don't specify an offset, so you're always re-loading the same first "n" records... If you have no more than 5, asking for 10 will give you the same 5 as before... – MacPrawn Feb 20 '17 at 21:10
  • There's 25 or so in there....that's not it. – Duck of Death Feb 20 '17 at 21:41
  • But are they all returned when you remove he posts_per_page attribute? I see you use 'post__in' => $what, could this limit you to less than 10 posts? – MacPrawn Feb 20 '17 at 21:46
  • No I can load all of them if I want, I can load all 25 if I put 25 in posts_per_page. And I can set the variable to 25 and that will work as well. – Duck of Death Feb 20 '17 at 21:52
  • where is this happening increment it by 10 when the button is clicked ? – David Lee Feb 20 '17 at 23:03
0

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!

  • Thank you this is close enough to count as an answer I think. Sheepishly I have to admit that my code was part of the issue. THe array $what is supposed to contain all the IDs already loaded for sorting purposes, but of course it was preventing me from getting the other results. You are correct that the right way to handle it is to use the offset, but of couse now instead of siimply loading the div and over writing, I am appending to the results already there. Thanx. – Duck of Death Feb 21 '17 at 17:06
0

You are assigning the value each time, you need to do a sum, the previous value plus the new 5 also if the page is refreshing (i am not sure if you are using ajax) you need to keep that value, something like this:

if(isset( $_POST['load-next-amount'] )){
     //the previous value plus 5
     $previous_value = $_POST['load-next-amount'];
     $pageamount = $previous_value + 5; 
}
 else {
     $pageamount = 5;   
 }

in the URL of the button you need to put the var, something like this:

<a href="?load-next-amount=<?php echo $pageamount; ?>"> Load More </a>
  • Yes should have mentioned am using AJAX. I have done it as you suggest, it didn't work. I only have the above code to express the issue, it's not the final code. The only way the variable actually changes the number of posts is if it is LESS. – Duck of Death Feb 20 '17 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.