Add Feature To Registered Post Type

All of our lovely content in WordPress (except widgets) is stored in our database as a “post type.” This could be a page, post, navigation menu or something custom like a staff member, food menu item or product. Most of the time these work great out of the box, but occasionally you may need a feature that’s part of one post type, added to another one.

Say you’re creating a site that will combine WordPress posts and pages into one spot. You’ll need to be able to set the order of them. That’s no problem for pages; WordPress has an order field for them by default, but not posts. To add that feature to posts, you simply need to add this to your theme’s functions.php file:

add_post_type_support( 'post', 'page-attributes' );

and now you can enter an order number on your posts and arrange them in a mixed query with your pages.

Let’s take this a bit further. Say you’re working with a plugin that has a custom post type registered already, but it’s lacking support for custom fields. You may need custom fields in order to extend the plugin’s capabilities. Most of the time you add in support for specific features when you register a custom post type. But you can add support for another feature outside of when you register the post type. You just use a snippet like this:

add_post_type_support( 'slide', 'custom-fields' );

In this case slide is the name given to the post type and custom-fields is the feature we’re wanting to add to it. The Codex has a list of available options that you can use to add quite a bit of additional functionality if you need it.

That’s all there is to it. This will also work great in a child theme or site specific plugin too.

Enjoy.

Submit your comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get all themes with lifetime updates for 95 USD.View All Promotions
+ +