Different post format options per custom post type?

I want to add post formats for each post type. For example, I have a post type ‘Gallery’. I want to add ‘images’, ‘galleries’ and ‘videos’ in this post. In normal posts I want to use another list of post formats.

I try:

Read More
function postf()
{
    global $post_ID;

    $postType = get_post_type( $post_ID );

    if( $_GET['post_type'] || $postType == 'gallery-custompost' )
    {
        add_theme_support( 'post-formats', array( 'image', 'gallery', 'video' ) );
        add_post_type_support( 'gallery-custompost', 'post-formats' );
    }
}
add_action('init', 'postf');

When I add a new post, it works, but when I try to edit it, the post format does not appear.

Does anyone have any idea how I must do it?

Related posts

Leave a Reply

3 comments

  1. Try adding the post format support on the arguments when you register your post type instead of using add_post_type_support, like this for example:

    $args = array(
        ...
        'supports' => array('title', 'editor', 'author', 'post-formats')
    ); 
    register_post_type('gallery-custompost', $args);
    

    See if have any luck.

  2. You can change the list that appears in the post format meta box, but when you save the post the submitted value is checked against the registered post formats, and your code probably did not run there.

    My solution is to register all post formats that you will use with your theme, and then limit them based on the post type by replacing the standard meta box with one of your own. This will only limit the available options in the meta box, it won’t restrict them when you save a post, but you can probably add extra checking to prevent adventurous users from setting “disallowed” post formats.

    // Register all post types that we will use
    add_action( 'after_setup_theme', 'wpse16136_after_setup_theme', 11 );
    function wpse16136_after_setup_theme()
    {
        add_theme_support( 'post-formats', array( 'aside', 'gallery', 'video' ) );
    }
    
    // Register our custom post type, and link the post formats to the post types
    // Yes, you can (ab)use add_post_type_support to save extra data like this
    add_action( 'init', 'wpse16136_init' );
    function wpse16136_init()
    {
        register_post_type( 'wpse16136', array(
            'public' => true,
            'label' => 'WPSE 16136',
            'supports' => array( 'title', 'editor' ),
        ) );
        add_post_type_support( 'wpse16136', 'post-formats', array( 'gallery', 'video' ) );
        add_post_type_support( 'post', 'post-formats', array( 'aside', 'gallery' ) );
    }
    
    // Replace the standard meta box callback with our own
    add_action( 'add_meta_boxes', 'wpse16136_add_meta_boxes' );
    function wpse16136_add_meta_boxes( $post_type )
    {
        if ( ! get_post_type_object( $post_type ) ) {
            // It's a comment or a link, or something else
            return;
        }
        remove_meta_box( 'formatdiv', $post_type, 'side' );
        add_meta_box( 'wpse16136_formatdiv', _x( 'Format', 'post format' ), 'wpse16136_post_format_meta_box', $post_type, 'side', 'core' );
    }
    
    // This is almost a duplicate of the original meta box
    function wpse16136_post_format_meta_box( $post, $box ) {
        if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) ) :
        $post_formats = get_theme_support( 'post-formats' );
    
        // This is our extra code
        // If the post type has registered post formats, use those instead
        if ( is_array( $GLOBALS['_wp_post_type_features'][$post->post_type]['post-formats'] ) ) {
            $post_formats = $GLOBALS['_wp_post_type_features'][$post->post_type]['post-formats'];
        }
    
        if ( is_array( $post_formats[0] ) ) :
            $post_format = get_post_format( $post->ID );
            if ( !$post_format )
                $post_format = '0';
            $post_format_display = get_post_format_string( $post_format );
            // Add in the current one if it isn't there yet, in case the current theme doesn't support it
            if ( $post_format && !in_array( $post_format, $post_formats[0] ) )
                $post_formats[0][] = $post_format;
        ?>
        <div id="post-formats-select">
            <input type="radio" name="post_format" class="post-format" id="post-format-0" value="0" <?php checked( $post_format, '0' ); ?> /> <label for="post-format-0"><?php _e('Standard'); ?></label>
            <?php foreach ( $post_formats[0] as $format ) : ?>
            <br /><input type="radio" name="post_format" class="post-format" id="post-format-<?php echo esc_attr( $format ); ?>" value="<?php echo esc_attr( $format ); ?>" <?php checked( $post_format, $format ); ?> /> <label for="post-format-<?php echo esc_attr( $format ); ?>"><?php echo esc_html( get_post_format_string( $format ) ); ?></label>
            <?php endforeach; ?><br />
        </div>
        <?php endif; endif;
    }
    
  3. Post Formats are a custom taxonomy, registered for the “Post” post-type. If you want to use the “Post Format” taxonomy for a different post-type, then you’ll need to register it for that post-type, rather than merely enable support for it for the “Post” post-type.