WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 4 years ago

#30833 closed defect (bug) (invalid)

Post type object does not always have taxonomies populated correctly.

Reported by: desrosj Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Taxonomy Keywords: has-patch needs-testing close
Focuses: Cc:

Description

Using get_post_type_object(), I found that the 'taxonomies' array was not always accurate with all added taxonomies.

For example, if you do the following, get_post_type_object( 'post' ), there should be 'category', 'post_tag', and 'post_format' values (on a fresh install of WordPress) in that array.

The only time taxonomies seem to make it into there is when you define taxonomies in the 'taxonomies' argument when using register_post_type().

The attached patch fixes this issue. However, some additional thought may need to be given. Was this post type object property meant to be used like that? Do we only populate it with public taxonomies? Do we add a parameter to register_taxonomy() to control if it gets listed there?

Attachments (1)

post-type-object-taxonomies.patch (911 bytes) - added by desrosj 5 years ago.
Patch adding taxonomies to post type objects when using register_taxonomy().

Download all attachments as: .zip

Change History (4)

@desrosj
5 years ago

Patch adding taxonomies to post type objects when using register_taxonomy().

#1 @desrosj
5 years ago

  • Keywords has-patch needs-testing added

#2 @nacin
5 years ago

  • Keywords close added
  • Version changed from 4.1 to 3.0

The 'taxonomies' property is actually only for registration. We probably should unset it after we call register_taxonomy_for_object_type() internally.

Post types associated with a taxonomy are stored within the taxonomy object. You can get them by doing this:

$object_types = get_taxonomy( $taxonomy )->object_types;
// object types could include 'user', 'link', or something arbitrary
$object_types = array_filter( $object_types, 'post_type_exists' );

Eventually, post type objects and taxonomy objects will be proper objects (no timeline for this — we have a long way to go before this makes sense and the picture is clear), at which point helper methods may make this easier, and would allow for live-updating at any given time.

#3 @boonebgorges
4 years ago

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

Closing based on nacin's comment above.

Note: See TracTickets for help on using tickets.