[wp-trac] [WordPress Trac] #30846: Custom query using tag__in sets query_var tag_id

WordPress Trac noreply at wordpress.org
Fri Dec 26 15:59:25 UTC 2014


#30846: Custom query using tag__in sets query_var tag_id
--------------------------+-----------------------------
 Reporter:  philbirnie    |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  4.1
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 When creating a custom query and using the `tag__in` parameter,
 `WP_Query::get_posts()` sets `tag_id` to the first id in the `tag__in`
 array.  This renders the `tag__in` array useless. For example, if the
 first tag id in the `tag__in` array has no posts; an empty array is
 returned.

 === To recreate: ===

 1. Create 3 or more tags (in my example, I'll refer to them as "A" with ID
 2, "B",  with ID 3 and "C" with ID 4.
 2. Assign posts to "A" and "B", but do NOT assign any posts to "C".  (Tag
 "C" should have no posts associated with it)
 3. Create a custom query using the `tag__in` parameter, like so:


 {{{
                 $queryArgs = array(
                         'tag__in' => array(4,3,2)
                 );

                 $query = new WP_Query($queryArgs);

                 var_dump($query->query_vars);

                 var_dump($query->get_posts());


 }}}

 4. When looping through the query, you'll note that there will be no
 posts.
 5. Similarly `$query->get_posts()` returns an empty array.
 6. `$query->query_vars`  will show that `tag__in` is set correctly, but
 the `tag_id` parameter is also set to the first element in the `tag__in`
 array.

 ==== `var_dump($query->query_vars)` output from the code above====

 (Irrelevant parts of array removed, denoted by ellipses)

 {{{
 array (size=61)
   'tag__in' =>
     array (size=3)
       0 => int 4
       1 => int 3
       2 => int 2
   'error' => string '' (length=0)
   'm' => string '' (length=0)
   ...
   'tag' => string '' (length=0)
   'cat' => string '' (length=0)
   'tag_id' => int 4
   ...
   'category__in' =>
     array (size=0)
       empty
   'category__not_in' =>
     array (size=0)
       empty
   'category__and' =>
     array (size=0)
       empty
   'post__in' =>
     array (size=0)
       empty
   'post__not_in' =>
     array (size=0)
       empty
   'tag__not_in' =>
     array (size=0)
       empty
   'tag__and' =>
     array (size=0)
       empty
   'tag_slug__in' =>
     array (size=0)
       empty
   'tag_slug__and' =>
     array (size=0)
       empty
   'post_parent__in' =>
     array (size=0)
       empty
    ...
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/30846>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list