How to set a default format for a custom post type?

I created a custom post type for my blog, to allow easier separation of content. This new post type supports different post formats, but most of them will be galleries.

register_post_type('atelier',
    array(
    'label' => 'L'Atelier',
    'public' => true,
    'supports' => array('title', 'editor', 'post-formats')
    )
);

I saw that it is possible in Settings -> Writing to set the default post format for posts, is it possible to do the same for my newly created post type?

Related posts

Leave a Reply

3 comments

  1. One option would be to modify the global Default Post Format setting, via Dashboard -> Settings -> Writing.

    Note that this setting is global, so it would set the default for all post types that support Post Formats.

    If you have no need of post formats for blog Posts, you could simply enable post-format support only for your custom post type, by removing post-format support for blog posts:

    <?php
    remove_post_type_support( 'post', 'post-formats' );
    ?>
    

    (Untested, but I see no reason why it shouldn’t work.)

  2. You can handle this using the option_default_post_format filter:

    add_filter( 'option_default_post_format', 'slimline_default_post_format' );
    
    /**
     * Posts of post_type_1 will be asides by default, but all other post types
     * will be the default set on the Settings > Writing admin panel
     */
    function slimline_default_post_format( $format ) {
        global $post_type;
    
        return ( 'post_type_1' === $post_type ? 'aside' : $format );
    }
    

    If you want to set up the filter for multiple custom post types, I’d edit the function to use a switch statement, like so:

    function slimline_default_post_format( $format ) {
        global $post_type;
    
        switch( $post_type ) {
            case 'post_type_1' :
                $format = 'aside';
                break;
            case 'post_type_2' :
                $format = 'quote';
                break;
        }
    
        return $format;
    }
    
  3. For starters used to a different PHP syntax: The code blocks in @MichaelDozark’s excellent answer can also be written as:

    /*
     * Posts of post_type_1 will be asides by default, but all other post types
     * will be the default set on the Settings > Writing admin panel
     */
    add_filter( 'option_default_post_format', 'custom_default_post_format' );
    function custom_default_post_format( $format ) {
        global $post_type;
    
        if( $post_type == 'post_type_1' ) {
            $format = 'aside';
        }
    
        return $format;
    }
    

    And:

    add_filter( 'option_default_post_format', 'custom_default_post_format' );
    function custom_default_post_format( $format ) {
        global $post_type;
    
        if( $post_type == 'post_type_1' ) {
            $format = 'aside';
        } elseif ( $post_type == 'post_type_2' ) {
            $format = 'quote';
        }
    
        return $format;
    }
    

    Source: Examples for the switch statement in the PHP Manual.