Make WordPress Core

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#17620 closed defect (bug) (worksforme)

get_post_types check for supporting title

Reported by: garyj's profile GaryJ Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.2
Component: Posts, Post Types Keywords: close
Focuses: Cc:

Description

In 3.2-beta2-18055, a print_r of:

get_post_types();

correctly returns:

Array ( [post] => post [page] => page [attachment] => attachment [revision] => revision [nav_menu_item] => nav_menu_item [surl] => surl [book] => book )

(Default post types, plus a couple of my extra ones).

However, a print_r of:

get_post_types( array( 'supports' => array( 'title' ) ) );

returns:

Array ( [attachment] => attachment [revision] => revision [nav_menu_item] => nav_menu_item )

instead of the ones that actually have support for the title feature.
The same result occurs when checking for editor support. I've tried my registered post types with and without title support, yet neither seem to appear when filtering by title support, not doe post or page, which I would also expect to be returned.

Change History (5)

#1 follow-up: @greuben
13 years ago

  • Keywords close added
get_post_types( array( 'supports' => array( 'title' ) ) );

The default operator for get_post_types is 'AND' so it will match post types which only support title

Use 'OR' operator if you want all post types that support titles.

get_post_types( array( 'supports' => array( 'title' ) ), 'names', 'or' )

#2 in reply to: ↑ 1 @GaryJ
13 years ago

  • Severity changed from major to normal

Replying to greuben:

Use 'OR' operator if you want all post types that support titles.

get_post_types( array( 'supports' => array( 'title' ) ), 'names', 'or' )

That results in:

Array
(
    [post] => post
    [page] => page
    [attachment] => attachment
    [revision] => revision
    [nav_menu_item] => nav_menu_item
    [surl] => surl
    [book] => book
)

My book CPT doesn't have support for title, yet is still included in the list.

@Nacin says that get_post_types() can't check for support, since it's an array, rather than a string/int/bool. The solution is to grab them all and loop through them and use post_type_supports().

Not a bug then - guess I was just trying to do something that hasn't been added to core, yet :-)

#3 @scribu
13 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

get_post_types( array( 'supports' => array( 'title' ) ), regardless of operator, will only match post types that only support 'title'.

Similarly, get_post_types( array( 'supports' => array( 'title', 'editor' ) ) will only match post types that only support 'title' and 'editor'. No more, no less.

Since there's no elegant way to define the operator between elements in the 'supports' array, you will just have to loop through all post types, using post_type_supports() to filter them.

Last edited 13 years ago by scribu (previous) (diff)

#4 @scribu
13 years ago

Example:

$filtered = array();
foreach ( get_post_types() as $post_type ) {
  if ( post_type_supports( $post_type, 'title' ) ) {
    $filtered[] = $post_type;
  }
}
Last edited 13 years ago by scribu (previous) (diff)

#5 @GaryJ
13 years ago

Noted, thanks scribu.

Note: See TracTickets for help on using tickets.