Make WordPress Core

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#34010 closed enhancement (fixed)

Add Ability to Fetch Post Types by Supports

Reported by: wpsmith's profile wpsmith Owned by: swissspidy's profile swissspidy
Milestone: 4.5 Priority: normal
Severity: normal Version: 4.4
Component: Posts, Post Types Keywords: has-patch has-unit-tests commit
Focuses: Cc:

Description

I would be great to see a WordPress function or functions to fetch a list of registered post types that support a specific feature or a set of features.

https://gist.github.com/wpsmith/6d6679e05c1ab62752ea

Attachments (8)

34010a.post.php.patch (1.1 KB) - added by wpsmith 8 years ago.
get_post_types_by_support()
34010b.post.php.patch (1.4 KB) - added by wpsmith 8 years ago.
get_post_types_by_supports()
34010.diff (868 bytes) - added by swissspidy 8 years ago.
34010.multiple-features.diff (805 bytes) - added by barryceelen 8 years ago.
Allow getting post types by multiple features
34010.2.diff (1.9 KB) - added by swissspidy 8 years ago.
34010.with-operator.diff (1.1 KB) - added by barryceelen 8 years ago.
Include and/or operator
34010.3.diff (2.8 KB) - added by swissspidy 8 years ago.
34010.4.diff (3.3 KB) - added by swissspidy 8 years ago.

Download all attachments as: .zip

Change History (24)

#1 @wpsmith
8 years ago

  • Keywords has-patch dev-feedback 2nd-opinion needs-testing needs-unit-tests added

@wpsmith
8 years ago

get_post_types_by_support()

@wpsmith
8 years ago

get_post_types_by_supports()

@swissspidy
8 years ago

#2 @swissspidy
8 years ago

  • Keywords 2nd-opinion needs-testing removed

It's not uncommon to loop through to all post types to see if a post type supports a given feature, so this is certainly of value in my opinion. I don't think it's necessary to query by multiple features though.

34010.diff proposes a simple function get_post_types_by_support( $feature ) function (get_post_types_supporting sounds good too). Basically like in this gist, but without needing to access the global variable.

@barryceelen
8 years ago

Allow getting post types by multiple features

#3 @barryceelen
8 years ago

@swissspidy Why are you looping through $post_types in stead of using the $_wp_post_type_features global? I see all other post_type_support related functions are using it.

Last edited 8 years ago by barryceelen (previous) (diff)

@swissspidy
8 years ago

#4 @swissspidy
8 years ago

  • Keywords has-unit-tests added; dev-feedback needs-unit-tests removed
  • Milestone changed from Awaiting Review to 4.5

@barryceelen I don't know anymore, probably just to not rely on the global :)

I don't think it's necessary to query by multiple features though.

Since it's so simple and there are situations where this can indeed be necessary, I changed my mind here. I just uploaded 34010.2.diff now which includes unit tests.

#5 follow-up: @barryceelen
8 years ago

:)
While we're at it might as well also add the $operator parameter like get_post_types() does (One of the patches by @wpsmith had it but the server is unreachable at the time).

@barryceelen
8 years ago

Include and/or operator

#6 in reply to: ↑ 5 @swissspidy
8 years ago

Replying to barryceelen:

:)
While we're at it might as well also add the $operator parameter like get_post_types() does (One of the patches by @wpsmith had it but the server is unreachable at the time).

Why don't we just allow passing a 'supports' filter to get_post_types()? Makes more sense IMHO.

#7 @barryceelen
8 years ago

In fact, the get_post_types() function is where I started looking for this functionality, kinda makes sense to me. Something held me back from modifying that function but I can't recall why :)

Last edited 8 years ago by barryceelen (previous) (diff)

#8 @swissspidy
8 years ago

  • Focuses administration template removed
  • Keywords needs-refresh added

Adding this to get_post_types() seems to make more sense, but it would also make the function more complicated.

Let's stick with 34010.2.diff with the operator argument suggested in 34010.with-operator.diff.

#9 @barryceelen
8 years ago

Thanks for getting back to this. Adapting the get_post_types() function indeed got unwieldy when trying to combine/diff the two different $_wp_post_type_features and $wp_post_types arrays. Also, the $_wp_post_type_features array contains 'meta' post types like attachment.video and attachment.audio, unsure how those would be handled.

#10 @swissspidy
8 years ago

  • Keywords 2nd-opinion added

This ticket was mentioned in Slack in #core by chriscct7. View the logs.


8 years ago

@swissspidy
8 years ago

#12 @swissspidy
8 years ago

  • Keywords needs-refresh 2nd-opinion removed

34010.3.diff is an updated patch based on my last comment.

#13 @ocean90
8 years ago

  • Keywords commit added
  • Owner set to swissspidy
  • Status changed from new to assigned

Patch looks good.

@swissspidy
8 years ago

#14 @swissspidy
8 years ago

34010.4.diff uses assertContains instead of assertEqualSets because some other tests register some random post types, causing these tests to fail.

#15 @swissspidy
8 years ago

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

In 36652:

Posts: Introduce get_post_types_by_support().

Similar to get_post_types(), this new function returns a list of post type names that support a specific feature.

Props wpsmith, barryceelen, swissspidy.
Fixes #34010.

#16 @DrewAPicture
8 years ago

In 36704:

Posts: Rename the $args parameter in get_post_types_by_support() to $feature for better self-documentation.

See #34010. See #32246.

Note: See TracTickets for help on using tickets.