Make WordPress Core

Opened 9 years ago

Closed 9 years ago

#8987 closed defect (bug) (invalid)

add_action('pre_get_posts') cannot use 'tag' and 'cat' together

Reported by: galbustein Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.7
Component: General Keywords:
Focuses: Cc:


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.

Change History (1)

#1 @Denis-de-Bernardy
9 years ago

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

if you change the query var, WP will use one or the other. use pre_get_posts to mark the query as "in need of change" and then a separate filter, on posts_join for instance

Note: See TracTickets for help on using tickets.