Exclude WordPress post types of add_meta_box()

Most of you have added custom meta boxes to WordPress using add_meta_box(). Normally it looks something like this:

1 2 3 4 56 7 8 9 1011 12 
function meta_box_picture() {     add_meta_box(     'my-meta-box',                // ID attribute of metabox     'My title',                   // Title of metabox visible to user    'my-meta_box_callback',       // Function that prints box in wp-admin     'page',                       // Show box for posts, pages, custom, etc.     'up',                         // Where on the page to show the box, change to something other then normal, advanced or side     'high'                        // Priority of box in display order   );} add_action( 'add_meta_boxes', 'meta_box_picture' );

Our focus will be the 4th argument. In the above example the meta box will only be rendered for the page edit screen. You could also supply an array. For example $screen= array( 'page', 'post', 'myposttype') and fill this in instead of 'page' .
Your meta box will now be rendered at the edit screens of page, post, and your own posttype.

Exclude a posttype

That's all great, but if one day you decide to create a new post type, you'll have to dive back in you code to add your new posttype to the $screen array.
You could add the metabox to all post types simply by leaving the 4th argument empty '' .

But what if you want them to all post types except one or two. There is no exclude argument, but we can make one!.
It's pretty simple. Like this:

1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 
function meta_box_picture() {   $excludeScreen= array('armag');           // The posttypes you want to exclude   $CurrentScreen= get_current_screen();     // Get the current edit screen   $screen= in_array( $CurrentScreen->id, $excludeScreen) ? 'exclude' : '';   // If the current screen id is in the exclude array set $screen to 'exclude'  // Instead of exclude you can use any word as long it's not a posttype or empty   // If the current screen id is not in the exclude array set $screen to ''     add_meta_box(     'my-meta-box',                // ID attribute of metabox    'My title',                   // Title of metabox visible to user     'my-meta_box_callback',       // Function that prints box in wp-admin     $screen,                      // Show box for posts, pages, custom, etc.     'up',                         // Where on the page to show the box, change to something other then normal, advanced or side     'high'                        // Priority of box in display order  ); } add_action( 'add_meta_boxes', 'meta_box_picture' );

That's all! I hope it's helpful. If so, or you have questions, please leave a comment 🙂

  • I apologize for my poor English. I should have paid more attention in school...
    If there are any huge mistakes please inform me
  • I've never studied C, Javascript or PHP. Everything I know is learned via youtube and google.
    I realize my code is often unnecessarily long, but I prefer writing it this way so I can still understand myself later on. If there are other mistakes please let me know.