Opened 13 months ago
Last modified 2 months ago
#20633 new defect (bug)
wp-includes/comment.php:738 - Undefined property: WP_Query::$comments
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | 3.6 |
| Component: | Warnings/Notices | Version: | 3.3.2 |
| Severity: | normal | Keywords: | has-patch dev-feedback |
| Cc: |
Description
get_comment_pages_count() can throw a notice. We use $wp_query->comments without checking it first.
Attachments (3)
Change History (10)
comment:1
markjaquith — 13 months ago
- Version set to 3.3.2
markjaquith — 13 months ago
comment:2
markjaquith — 13 months ago
comment:3
SergeyBiryukov — 13 months ago
- Keywords has-patch added
The patch is not correct. It does this:
- if ( !$comments || !is_array($comments) )
+ if ( ( !$comments || !is_array($comments) ) && !empty( $wp_query->comments ) )
$comments = $wp_query->comments;
if ( empty($comments) )
return 0;
The patch defeats the is_array() check. For example, $comments=true, $wp_query->comments=null will not return 0.
Suggest:
- if ( !$comments || !is_array($comments) )
+ if ( empty($comments) || !is_array($comments) ) {
+ if (empty( $wp_query->comments ) )
+ return 0;
$comments = $wp_query->comments;
+ }
- if ( empty($comments) )
- return 0;
comment:5
in reply to:
↑ 4
DrewAPicture — 2 months ago
- Keywords dev-feedback added
Easiest place to see this bug is before the Loop. I put a note about that in the Codex, since then the function returns 0 with or without the bug fix. http://codex.wordpress.org/index.php?title=Function_Reference%2Fget_comment_pages_count&diff=128919&oldid=126895
A test for this bug is to set WP_DEBUG to true. In wp-content/themes/twentythirteen/single.php insert
<?php printf('<pre>get_comment_pages_count=%s</pre>',
get_comment_pages_count()); ?>
before and after the loop starts, at
<?php comments_template(); ?>
And look at the Hello World post.
Expect 0 then 1, with 1 warning before the bug and no warnings after.
Another test is to make sure
<?php printf('<pre>get_comment_pages_count=%s</pre>',
get_comment_pages_count($GLOBALS['wp_query']->comments)); ?>
still works after the Loop. Expect 1.
But I didn't see anything similar in the unit tests as far as I could tell.
comment:7
SergeyBiryukov — 2 months ago
- Milestone changed from Awaiting Review to 3.6

Make sure it's not empty.