[wp-trac] [WordPress Trac] #43372: $wp_query->max_num_pages return value as float

WordPress Trac noreply at wordpress.org
Sat Mar 31 09:52:54 UTC 2018


#43372: $wp_query->max_num_pages return value as float
--------------------------+------------------------------
 Reporter:  ironghost63   |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Query         |     Version:  4.9.4
 Severity:  trivial       |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------

Comment (by birgire):

 @ironghost63 Welcome to WordPress Trac and thank you for the report.

 But there is more!

 The example:

 {{{
 $q = new WP_Query( ['posts_per_page' => 2 ] );
 var_dump( $q->post_count );
 var_dump( $q->found_posts );
 var_dump( $q->max_num_pages );

 }}}

 displays:

 {{{
 int(2)
 string(1) "2"
 double(1)

 }}}

 So these counting properties of {{{WP_Query}}} have three different types!

 Within the {{{WP_Query}}} class, the inline docs says it's an integer:

 {{{
 /**
  * The amount of pages.
  *
  * @since 2.1.0
  * @var int
  */
 public $max_num_pages = 0;
 }}}

 The calculated value comes from:

 {{{
 $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );

 }}}

 but from the [http://php.net/manual/en/function.ceil.php PHP docs]:

 > The return value of {{{ceil()}}} is still of type float as the value
 range of float is usually bigger than that of integer.


 The inline docs for {{{found_posts}}} also says:

 {{{
 /**
  * The amount of found posts for the current query.
  *
  * If limit clause was not used, equals $post_count.
  *
  * @since 2.1.0
  * @var int
  */
 public $found_posts = 0;

 }}}

 but the data query

 {{{
 $this->found_posts = $wpdb->get_var(...);

 }}}

 returns a string.

 So it looks like either we need to adjust the inline docs or adjust the
 return types.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/43372#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list