Make WordPress Core

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#14976 closed defect (bug) (fixed)

WP_Query's init() and init_query_flags() do not do thorough resets

Reported by: coffee2code's profile coffee2code Owned by:
Milestone: 3.1 Priority: normal
Severity: minor Version: 3.0.1
Component: Query Keywords: has-patch dev-feedback
Focuses: Cc:


Despite WP_Query's init() documented claim that it "initiates object properties and sets default values" and that init_query_flags() "resets query flags to false", both are not thorough in their tasks. This likely stems from these functions not always being kept in sync with changes made to WP_Query.

For init(), the following variables, as defined in the class, are not reset:

        var $request;
        var $post;
        var $comments;
        var $comment_count = 0;
        var $current_comment = -1;
        var $comment;
        var $found_posts = 0;
        var $max_num_pages = 0;
        var $max_num_comment_pages = 0;

For init_query_flags(), the following query flags, as defined in the class, are not reset:

        var $is_preview = false;
        var $is_comments_popup = false;

The attached patch fixes those functions to unset/reset all the above object variables.

And one additional minor thing: the class variable $is_paged was not explicitly defined as a class variable unlike all the other class variables, so the patch does so.

Attachments (1)

14976.diff (1.5 KB) - added by coffee2code 13 years ago.
Updated original patch

Download all attachments as: .zip

Change History (7)

#1 @scribu
13 years ago

You would also have to reset the newly introduced $tax_query and $meta_query.

13 years ago

Updated original patch

#2 @coffee2code
13 years ago

Ah, you're right. I missed those two. Updated the original patch to include them.

#3 @scribu
13 years ago

  • Milestone changed from Awaiting Review to 3.1

Looking good.

#4 @scribu
13 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [15672]) Do thorough resets in init() and init_query_flags(). Props coffee2code. Fixes #14976

#5 @hakre
13 years ago

  • Keywords dev-feedback added

I suggest an additional init_something subroutine for those values directly reset in init() to easier understand the init function.

Next to that I assume that it's not wanted to unset public properties but to set them to null or better to say, their default value. Unsetting the properties means to remove them from the class which I assume is not intended.

#6 @coffee2code
13 years ago

I agree that the unsetting of the public properties was a bit unusual. I personally would have set them to null. However, the existing convention in the function was to unset properties that were defined but not given an initial value, otherwise re-initialize those that were assigned an initial value. I figured the existing convention warranted a separate discussion, so I rolled with what was there. But I with you on that count.

Note: See TracTickets for help on using tickets.