WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#13109 closed enhancement (fixed)

get_taxonomies should filter object_type

Reported by: michaelh Owned by:
Milestone: 3.0 Priority: normal
Severity: normal Version: 3.0
Component: Posts, Post Types Keywords: has-patch commit
Focuses: Cc:

Description

get_taxonomies should filter on the object_type to allow retrieving all taxonomies for a particular post_type. Example:

$args=array(
  'object_type' => array('book','movie')
);
$output = 'objects'; 
$taxonomies=get_taxonomies($args,$output);

Note: get_object_taxonomies('book'); does return the taxonomy names related to an object_type / post_type

Attachments (3)

13109.diff (1.1 KB) - added by scribu 5 years ago.
wp_filter_object_list.diff (593 bytes) - added by scribu 5 years ago.
wp_filter_object_list.2.diff (700 bytes) - added by scribu 5 years ago.
added brackets

Download all attachments as: .zip

Change History (16)

comment:1 @scribu5 years ago

  • Keywords has-patch added
  • Milestone changed from Unassigned to 3.0

I often found it frustrating that get_object_taxonomies() only returned the taxonomy names.

Patch adds an $output parameter to get_object_taxonomies().

comment:2 @scribu5 years ago

To further filter the taxonomy objects you could do something like this:

`
$taxonomies = get_object_taxonomies('post', 'objects');

$taxonomies = wp_filter_object_list($taxonomies, array('_builtin' => false));
`

comment:3 @michaelh5 years ago

  • Keywords tested added

Thought you might change wp_filter_object_list to do that ;)

Okay tested that with both 'names' and 'objects' and it works.

Don't know why I had to apply that patch manually.

Thanks scribu.

@scribu5 years ago

comment:4 @scribu5 years ago

  • Keywords commit added

comment:5 @nacin5 years ago

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

(In [14479]) Allow get_object_taxonomies to return taxonomy objects. props scribu, fixes #13109.

comment:6 @nacin5 years ago

(In [14480]) Small fix to r14479. see #13109.

comment:7 @nacin5 years ago

Had to make a small change, at least one place in trunk checked for the [0] key. If you want to re-patch core to assume that the key is a name, instead of numeric, then feel free to re-open.

comment:8 @scribu5 years ago

Yeah, same as with #13246

comment:9 @scribu5 years ago

  • Keywords tested removed
  • Resolution fixed deleted
  • Status changed from closed to reopened

We should have consistent behaviour between get_object_taxonomies() and get_taxonomies().

wp_filter_object_list.diff makes it so that get_taxonomies(, 'names') also returns a numeric array.

comment:10 @nacin5 years ago

Missing some brackets there.

I did a quick scan and couldn't find any other times we use a key from get_post_stati, get_taxonomies, or get_post_types.

comment:11 @scribu5 years ago

Even so, it doesn't make much sense to return something like this:

`
array(

[category] => category,
[post_tag] => post_tag,
...

)
`

I meant get_taxonomies('', 'names')

comment:12 @nacin5 years ago

Hah, brackets are missing in the patch.

And I was simply confirming that no other places in core needed to be modified after this change.

@scribu5 years ago

added brackets

comment:13 @nacin5 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [14495]) Standardize returned array keys when output = 'names'. Return numeric keys. Standardizes get_object_taxonomies with get_taxonomies, get_post_types, and get_post_stati. props scribu. fixes #13109.

Note: See TracTickets for help on using tickets.