Make WordPress Core

Opened 15 years ago

Closed 14 years ago

#10907 closed defect (bug) (worksforme)

get_queried_object does not always return a category

Reported by: dimitryz's profile dimitryz Owned by: dd32's profile dd32
Milestone: Priority: normal
Severity: normal Version: 2.8.4
Component: Query Keywords: reporter-feedback
Focuses: Cc:


I've noticed that retrieving a category with WP_Query::get_queried_object() does not always work. When the query_var 'cat' is not set and 'category_name' is, nothing is returned. Shouldn't get_queried_object() also look in 'category_name' and, if present, retrieve the category using get_category_by_path()?

Here is a solution that seems appropriate:

function get_queried_object() {
if ($this->is_category) {
	$cat = $this->get('cat');
	if ($cat) {
		$category = &get_category($cat);
	} else {
		$cat = $this->get('category_name');
		$category = &get_category_by_path($cat);
	if ( is_wp_error( $category ) )
		return NULL;
	$this->queried_object = &$category;
	$this->queried_object_id = (int) $category->term_id;

Change History (3)

#1 @nacin
14 years ago

  • Owner changed from ryan to dd32
  • Status changed from new to assigned

#2 @dd32
14 years ago

  • Keywords reporter-feedback added; get_queried_object category removed
  • Milestone changed from Unassigned to 3.0

What sort of query are you using to cause this? Is it a specific URL?

for requests serving 'category_name', 'cat' should be set, as its already run through get_category_by_path by this code:

#3 @dd32
14 years ago

  • Milestone 3.0 deleted
  • Resolution set to worksforme
  • Status changed from assigned to closed

Please re-open with details on how to reproduce.

I cant work out a query using 'cat' which does not populate the Queried object correctly.

Note: See TracTickets for help on using tickets.