Different Post Formats per Post Type

WordPress has had post formats for a while now, and it’s a really useful feature. However, what if you want to allow different post formats for different post types? Well it’s actually pretty easy, but if you’re unclear on the difference between post formats and post types, then you should really start by reading Post Formats vs. Custom Post Types by Mark Jaquith.

A site I was recently working on wanted to use the “video” post format for regular posts and the “quote” post format in their “news” posts (these posts show like a news ticker). Here’s what I did to get it working:

function bd_remove_twentyeleven_options() {
	// We only support "quote" for News Items and "video" for posts
	add_theme_support( 'post-formats', array( 'quote', 'video' ) );
}
add_action( 'after_setup_theme','bd_remove_twentyeleven_options' );

function bd_adjust_post_formats() {
	if ( $post_id ) {
		$post = get_post( $post_id );
		if ( $post )
			$post_type = $post->post_type;
	} elseif ( !isset($_GET['post_type']) )
		$post_type = 'post';
	elseif ( in_array( $_GET['post_type'], get_post_types( array('show_ui' => true ) ) ) )
		$post_type = $_GET['post_type'];
	else
		return; // Page is going to fail anyway

	if ( 'news' == $post_type )
		add_theme_support( 'post-formats', array( 'quote' ) );
	elseif ( 'post' == $post_type )
		add_theme_support( 'post-formats', array( 'video' ) );
}
add_action( 'load-post.php','bd_adjust_post_formats' );
add_action( 'load-post-new.php','bd_adjust_post_formats' );

First, we set the post formats like usual. These will be overridden on the new post and post edit pages, but everywhere else we will say that we support both quote and video post formats.

Then we hook in to the post edit page and the new post page. The first chunk of if/elseif statements gets the post type by first checking the post (if we’re editing), then checking the $_GET variable.

Last we simply alter what post formats our theme says it supports based on the post type. In this case for news we change it to just ‘quote’ and for post we change it to just ‘video’. For now I leave other post types alone.

Simple, effective, and REALLY useful.

About Aaron D. Campbell

Owner and lead developer at BlueDog, Aaron has 10+ years of web development experience, it a regular core contributor to the WordPress project, and has released many WordPress plugins.
This entry was posted in Wordpress and tagged , , , . Bookmark the permalink.

2 thoughts on “Different Post Formats per Post Type

  1. I was looking for this solution and I found it, Thanks a lot.
    But I have to report an issue for me:

    in my implementation of that function and related “add_action” I had to replace lines:

    if ( $post_id ) {
    $post = get_post( $post_id );
    if ( $post )
    $post_type = $post->post_type;

    with:

    if (isset($_GET['post'])) {
    $post = get_post($_GET['post']);
    if ($post)
    $post_type = $post->post_type;

    that is because $post_id isn’t visible inside function.
    After do that all works as well!!!
    Thanks Aaron, bye!

  2. WPExplorer says:

    Works well, except this solution won’t work for quick edits.

Leave a Reply

Your email address will not be published. Required fields are marked *

Note: If you are replying to another commenter, click the "Reply to {NAME} ↵" button under their comment!