Set custom WordPress post format for custom post type
If you’re working with custom post types in WordPress, you may have noticed that all post formats for the default post type automatically apply to custom post types as well. Depending on the type of post, this does not always make sense to use the post formats such as “image”, “video”, “quote”, “link”, “gallery” or “audio”.
Which post formats are available?
- aside – Typically styled without a title. Similar to a Facebook note update..
- chat – A chat transcript.
- gallery – A gallery of images.
- link – A link to another site.
- image – A single image.
- quote – A quotation.
- status – A short status update, similar to a Twitter status update.
- video – A single video or video playlist.
- audio – An audio file or playlist.
Then of course there is the default post format, which is always selected if you have not defined any other. The most common post formats in WordPress are default, gallery, quote, video and audio. More information about the WordPress post formats can be found here.
How do I add custom post formats?
To add additional formats in addition to the default post format, you have to extend your theme functions.php file with the following code snippet:
Within the array you can define the new post formats to be supported.
How do I add my own post formats depending on the custom post type?
The problem is that all post formats defined for the default post type automatically apply to all other custom post types. For example, post formats like chat, quote or audio are less useful in a recipe post type, but can be still selected in the WordPress backend for this custom post type. This can confuse the user if, for example, the post formats for this post type are not formatted and displayed correctly.
The following procedure will help us with this dilemma. First, generally we do not define post formats, like in the example above. We restrict support for custom post formats depending on the custom post type.
Define post formats ONLY for default post type
In the following example we define the supported post formats exclusively for the default WordPress post type. Add this code snippet, eg in the functions.php of your theme:
Add post formats for custom post types only
Similar to the example above, we now define the supported post formats only for our custom post types. In this example, I only added the video and gallery post formats for the custom post type “recipe”.
The WordPress filters load-post.php
and load-post-new.php
ensure that our function will be applied when editing or adding a post.