[wp-trac] [WordPress Trac] #24142: Zero value for posts_per_page value in wp_query custom instance and for 'Blog pages show at most' option
WordPress Trac
noreply at wordpress.org
Tue Oct 17 11:06:36 UTC 2017
#24142: Zero value for posts_per_page value in wp_query custom instance and for
'Blog pages show at most' option
------------------------------------------+-----------------------------
Reporter: alexvorn2 | Owner:
Type: defect (bug) | Status: reopened
Priority: normal | Milestone: Future Release
Component: Query | Version:
Severity: normal | Resolution:
Keywords: needs-patch needs-unit-tests | Focuses:
------------------------------------------+-----------------------------
Comment (by birgire):
I agree that this is a strange behavior.
To better understand it, we can map it out for version 4.8.2:
{{{
value posts_per_page showposts posts_per_archive_page
posts_per_rss
--------------------------------------------------------------------------------------
null | default default default
default
'' | default default default
default
0 | default default default
default
'0' | default default default
default
true | none 1 none 1
false | default default default
default
10 | 10 10 10 10
'10' | 10 10 10 10
'string'| 1 1 default 1
array() | default default 1
default
-1 | none none none
none
-2 | 2 2 2 2
}}}
where default means the stored option's value.
So there are some strange inconsistencies, e.g. the {{{true}}} row ;-)
The scope? of the ticket seems to be adjust the the {{{0}}} and {{{'0'}}}
rows to:
{{{
value posts_per_page showposts posts_per_archive_page
posts_per_rss
-------------------------------------------------------------------------------
0 | 0 0 0 0
'0' | 0 0 0 0
}}}
I think these two row cases, should be the same as returning an empty
array from
the {{{posts_pre_query}}} filter,
https://core.trac.wordpress.org/browser/tags/4.8.2/src/wp-includes/class-
wp-query.php#L2781
so we don't have to run those db queries. Like using {{{$this->posts =
array()}}}.
We would need to check for e.g.
{{{
if( isset( $q['posts_per_page'] ) && ( 0 === $q['posts_per_page'] || '0'
=== $q['posts_per_page'] ) ) {
}}}
and something simlar for the other query variables.
We would also have to adjust this part accordingly:
https://core.trac.wordpress.org/browser/tags/4.8.2/src/wp-includes/class-
wp-query.php#L1785-L1798
i.e.
{{{
$q['posts_per_page'] = (int) $q['posts_per_page'];
if ( $q['posts_per_page'] < -1 )
$q['posts_per_page'] = abs($q['posts_per_page']);
elseif ( $q['posts_per_page'] == 0 )
$q['posts_per_page'] = 1;
}}}
The e.g. case of {{{'showposts' => 'string'}}} is a little bit tricky here
for detection, because of the previous {{{$q['showposts'] = (int)
$q['showposts'];}}}
Before writing any patches I think we should consider what the scope of
this ticket should be,
i.e. should it be to only adjust the {{{0}}} and {{{'0'}}} rows or if
should it be also touch the {{{true, false, 'string', array(), ...}}}
rows?
Hope this helps anyone that's going to tackle this problem.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/24142#comment:9>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list