Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#10907 closed defect (bug) (worksforme)

get_queried_object does not always return a category

Reported by: dimitryz Owned by: 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)

comment:1 @nacin6 years ago

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

comment:2 @dd326 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: http://core.trac.wordpress.org/browser/trunk/wp-includes/query.php#L1877

comment:3 @dd326 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.