Make WordPress Core

Opened 13 years ago

Last modified 3 years ago

#20438 reopened defect (bug)

Custom Post Types with Post Format support aren't registered against post_format taxonomy

Reported by: batmoo's profile batmoo Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: needs-codex
Focuses: Cc:

Description

When a custom post type adds support for post-formats, this doesn't actually register the post_format taxonomy for the post_type.

Here's a quick test:

add_action( 'init', function() {
        register_post_type( 'not-post', array( 'supports' => 'post-formats' ) );
        global $wp_taxonomies;
        var_dump( 'post supports post_format:', in_array( 'post', $wp_taxonomies[ 'post_format' ]->object_type ) );
        var_dump( 'not-post supports post_format:', in_array( 'not-post', $wp_taxonomies[ 'post_format' ]->object_type ) );
} );

The post_format taxonomy doesn't get returned when get_object_taxonomies is called for the custom post type and one side-effect of this is that the post_format_relationships cache does not get flushed by clean_object_term_cache leading to stale values when a post format is changed.

(The post post_type is immune to this because core registers the post_format taxonomy with it.)

When a post_type adds support for post-formats, an explicit association should be made between the post_type and the post_format taxonomy.

Change History (19)

#1 @danielbachhuber
13 years ago

  • Cc wordpress@… added

#2 @saracannon
13 years ago

  • Cc sararcannon@… added

#3 follow-up: @nacin
13 years ago

If you are going to add UI support for post formats, you should just add the object type to the taxonomy as well.

So:

add_post_type_support( 'page', 'post-formats' );
register_taxonomy_for_object_type( 'post-format', 'page' );

We discussed this in 3.1. Given that using post formats for something other than posts is generally not going to be a good idea (or at least a very uncommon one), I certainly think two lines is just fine, rather than one plus doing something to unnecessarily automate this in core.

Version 0, edited 13 years ago by nacin (next)

#4 @nacin
13 years ago

  • Keywords close added

#6 @iseulde
11 years ago

  • Component changed from General to Post Types

#7 @danielbachhuber
11 years ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

#8 @chriscct7
10 years ago

#32385 was marked as a duplicate.

#9 @chriscct7
10 years ago

#31394 was marked as a duplicate.

#10 @chriscct7
10 years ago

#32385 was marked as a duplicate.

#11 follow-up: @s0what
9 years ago

  • Version set to 4.1.3

adding

register_taxonomy_for_object_type( 'post-format', 'page' );

doesn't work for me, nor does replacing 'post-format' with 'post-formats' or 'post_format' and 'post_formats'.

#12 in reply to: ↑ 11 @DrewAPicture
9 years ago

Replying to s0what:

adding

register_taxonomy_for_object_type( 'post-format', 'page' );

doesn't work for me, nor does replacing 'post-format' with 'post-formats' or 'post_format' and 'post_formats'.

@nacin's example in comment:3 had a typo. The slug for the post formats taxonomy is post_format not post-format.

This should work for you:

register_taxonomy_for_object_type( 'post_format', 'page' );

#13 @DrewAPicture
9 years ago

  • Version 4.1.3 deleted

#14 @s0what
9 years ago

Thanks for the quick replay, but it does not help. I still get 404 page.

#15 @Arcath
8 years ago

  • Resolution wontfix deleted
  • Status changed from closed to reopened

I am still getting this error.

<?php
register_taxonomy_for_object_type('post_format', 'page');

Has not fixed the problem

Running

<?php
print_r(get_object_taxonomies('page'));

proves that the taxonomy has been registered on pages but I still get the 404 for previews.

#16 @Arcath
8 years ago

Never mind I found the solution.

I have updated the wiki with the solution. https://codex.wordpress.org/Post_Formats#Adding_Post_Type_Support

#17 @Arcath
8 years ago

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

#18 in reply to: ↑ 3 @cowshase
4 years ago

  • Keywords needs-codex added
  • Resolution invalid deleted
  • Status changed from closed to reopened

Replying to nacin:

If you are going to add UI support for post formats, you should just add the object type to the taxonomy as well.

So:

add_post_type_support( 'page', 'post-formats' );
register_taxonomy_for_object_type( 'post_format', 'page' );

We discussed this in 3.1. Given that using post formats for something other than posts is generally not going to be a good idea (or at least a very uncommon one), I certainly think two lines is just fine, rather than one plus doing something to unnecessarily automate this in core.

I think the documentation on https://wordpress.org/support/article/post-formats/#adding-post-type-support should be amended accordingly.

If you register a post type with post-format support, but do not also manually register the post_format taxonomy to it, you will get a "Page not Found" if you try to preview custom posts that have already been published. It took me days to investigate this, until I found the solution in this ticket.

I also think that using custom post types with post formats is not as uncommon anymore as it may have been 9 years ago. I use it for example to have two sepate sections on my site with different user roles and capabilities but similar content features. So, I think the best solution would be to reverse the initial decision, and to automatically call register_taxonomy_for_object_type() if the custom post type is registered with post-format support or the support is added later via add_post_type_support().

#19 @SergeyBiryukov
3 years ago

  • Milestone set to Awaiting Review
Note: See TracTickets for help on using tickets.