最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Function not returning correct value for found_posts filter

programmeradmin3浏览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.

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.

Share Improve this question edited Apr 21, 2020 at 16:12 TonyG asked Apr 19, 2020 at 22:24 TonyGTonyG 11 bronze badge 8
  • Did you try to write as return ($found_posts - $offset); instead although it seems not necessary according to php? 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. – 西門 正 Code Guy - JingCodeGuy 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. – 西門 正 Code Guy - JingCodeGuy 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. – 西門 正 Code Guy - JingCodeGuy Commented Apr 21, 2020 at 0:05
 |  Show 3 more comments

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论