0

I am trying to return found_posts value minus the offset and I am not getting the expected pagination.

  function homepage_offset_pagination( $found_posts, $query ) {

    //Define our offset again...
    $offset = 3;

    //Ensure we're modifying the right query object...
    if( $query->is_home() ) {

        $found_posts = $found_posts - $offset;

    }

    return $found_posts;

  }
  add_filter( 'found_posts', 'homepage_offset_pagination', 10, 2 );

When the function tries to subtract $offset from $found_posts, I do not get the correct pagination, however if I echo the value out before returning I get the correct value.

Also, if I substitute $found_posts - $offset with an int literal I do get correct pagination.

I don't know what I am doing wrong.

I would appreciate any help, thanks.

EDIT Removed the is_paged condition because that was removing pagination in the admin panel. This works that same without the bug.

8
  • Did you try to write as return ($found_posts - $offset); instead although it seems not necessary according to php.net? Or did you try to put the result into a varaible and return that variable? If the value is right, it should be the same. Commented Apr 20, 2020 at 1:49
  • You my also try to use gettype() to examine the type of variable? Just try to understand in different perspective. Commented Apr 20, 2020 at 3:30
  • $found_posts is a string and $offset is an integer, and yes, I tried the parenthesis, I tried casting, even though like you said, in PHP, a string minus an integer should automatically make it an integer.
    – TonyG
    Commented Apr 20, 2020 at 17:12
  • For context, specifically, the total number of posts is 37 as of now with a post per page of 17. I want to make pagination recognize only 34 to have the first pagination page have a limit of 17 and the subsequent pages have a limit of 20, therefore have 2 pages recognized instead of 3. The pagination only works correctly when I return an int literal of 34. $found_posts - $offset doesn't work and neither does $found_posts - 3, but $found_posts - 4 works, but this is an incorrect number to use.
    – TonyG
    Commented Apr 20, 2020 at 17:47
  • Yes, php have auto type and most of the cases are correct but not all. Did you try intval() to force the type to be both integer? eg `intval( $found_posts ) - $offset' so that it should be same as the literal value you tried to put. Because different version and settings of the code will affect the php automatic type case. Commented Apr 21, 2020 at 0:05

1 Answer 1

0

Ok, so I found out the found_posts filter runs 3 times on the homepage and the third echo within the innermost element says $found_posts equals to 38 which is accounting for an unpublished post. I only noticed the first echo that was printing 37 at the top of the page. So if I set the $offset to 4 in the filter it works fine. Not sure if this will produce errors later on, but this answers the initial question.

1
  • I suggest you find out why it run duplicately and see where it run. Remove those unwanted filters is wise for long run to avoid unexpected results. Commented Apr 24, 2020 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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