[wp-trac] [WordPress Trac] #8987: add_action('pre_get_posts') cannot use 'tag' and 'cat' together

WordPress Trac wp-trac at lists.automattic.com
Thu Jan 29 11:09:50 GMT 2009


#8987: add_action('pre_get_posts') cannot use 'tag' and 'cat' together
--------------------------+-------------------------------------------------
 Reporter:  galbustein    |       Owner:  anonymous
     Type:  defect (bug)  |      Status:  new      
 Priority:  normal        |   Milestone:           
Component:  General       |     Version:  2.7      
 Severity:  normal        |    Keywords:           
--------------------------+-------------------------------------------------
 I have used pre_get_posts to limit the categories displayed to a list of
 predetermined categories.

 When I use this for "posts tagged with" using 'tag'/'tag_in' with
 $query->query_vars/$query->set there are never any results produced.

 If the 'tag'/'tag_in' lines are removed the code runs fine.

 Sample code block below:

 {{{
 public function postFilter($query) {

         $displayCategories = array("1","2","3","4");

         $displayCategoriesString = implode(",", $displayCategories);

         if ($query->is_tag) {

                 // FIXME: limit tagged post results to current locale

                 // I tried using query_vars initially
                 $query->query_vars['cat'] = $displayCategoriesString;
                 $query->query_vars['tag'] = 'testtag';

                 // then I tried using $query->set (uncomment for demo)
                 # $query->set('cat', $displayCategoriesString);
                 # $query->set('tag', array('testtag'));
                 # $query->set('tag__in', array('testtag'));

                 // uncomment next line to dump data
                 # echo "<pre>"; die(var_dump($query)); die;

         } else {
                 $query->query_vars['cat'] = $displayCategoriesString;
         }
         return $query;
 }

 // this method is used inside a class so &$this this used
 add_action('pre_get_posts', array(&$this, 'postFilter'));
 }}}


 I am using Wordpress 2.7.

-- 
Ticket URL: <http://trac.wordpress.org/ticket/8987>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list