[wp-trac] [WordPress Trac] #36381: query_posts neglecting other parameters when 'name' is used

WordPress Trac noreply at wordpress.org
Thu Mar 31 13:53:37 UTC 2016


#36381: query_posts neglecting other parameters when 'name' is used
-----------------------------+----------------------
 Reporter:  ioweuiraosjfosj  |       Owner:
     Type:  defect (bug)     |      Status:  closed
 Priority:  normal           |   Milestone:
Component:  Query            |     Version:  4.4.2
 Severity:  normal           |  Resolution:  invalid
 Keywords:                   |     Focuses:
-----------------------------+----------------------
Changes (by boonebgorges):

 * status:  new => closed
 * resolution:   => invalid
 * milestone:  Awaiting Review =>


Comment:

 Hi @ioweuiraosjfosj - Thanks for the report.

 The behavior you've described is by design. Providing the `'name'`
 parameter to `WP_Query` overrides other parameters, and performs a single-
 post query for an item where `post_name = $name`.

 If you need a non-exclusive way to narrow down posts by `post_name`, try
 `post_name__in`:

 {{{
 $q = new WP_Query( array(
     'post_name__in' => 'hello-world',
     'category_name' => 'category',
 ) );
 while ( $q->have_posts() ) :
     $q->the_post();
     // ...
 endwhile;
 }}}

 Side note: it's almost always incorrect to use `query_posts()` in a theme
 or plugin. Create a new `WP_Query` object as shown above.
 https://codex.wordpress.org/Function_Reference/query_posts

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


More information about the wp-trac mailing list