Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Dynamically Add Meta Boxes to Custom Post Types WordPress


As the title states, I would like to know if there is a way to dynamically display custom meta boxes for custom post types. I know that you can set it manually to display for specific post types, but is there a way to have the meta boxes be displayed dynamically for any custom post type? If so, I would love to see the code :)


Answers (1)


Utkarsh Kukreti answers:

You could use something like this to add a meta box to all the registered post types with public = true

foreach( get_post_types( array( 'public' => true ) ) as $post_type ) {
// skip for pages
if( $post_type == 'page' ) {
add_meta_box( 'id', 'title, 'callback', $post_type );

Reference for the callback functions,

Thomas Griffin comments:

I actually just came up with that right before I saw your post, so thanks for your post it was a solidifier for me. Now I am have another interesting issue. I actually don't want these meta boxes showing up on the Page screen admin, only for posts and custom post types. Is there any sort of conditional to check if I am currently on the Edit Page screen and if so to return false?

Utkarsh Kukreti comments:

Updated my answer.

Thomas Griffin comments:

Perfect! Thanks a ton :)

Thomas Griffin comments:

I would reward the money but there is some issue currently. I'll reward you as soon as it is fixed.