0

I'd like to add a meta box to all my pages (not posts) but it looks like I am only able to add it for a specific post based on the codex?

add_meta_box( $id, $title, $callback, $page, $context = 'advanced', $priority = 'default', $callback_args = null );
1

Try this. This is part of some code I use.

 // Add meta boxes to Page
  add_meta_box( 'RelatedImage', __('Related images'), 'related_images_meta_box', 'page', 'normal', 'high' );

  function related_images_meta_box($object) { 
    $nonce = wp_create_nonce(plugin_basename( __FILE__ ));
  ?>
    <input type="hidden" name="wp_filebrowser_nonce" id="wp_filebrowser_nonce" value="<?php echo $nonce; ?>" />
    <table>
      <tr>
        <td>
          <label for="related_image" class="">Related image</label>
        </td>
        <td>
          <input type="text" id="related_image" name="related_image"  value="<?php echo 'your meta property' ?>" size="40" />  
        <td>
      </tr>
    </table>
  <?php }
  • This only add to the "page" post type. – kaiser Mar 9 '12 at 2:01
  • 2
    Isn't that what the OP is asking for (to add a meta box to pages, but not to (blog) posts)? Or am I misreading the question? – Chip Bennett Mar 9 '12 at 2:10
  • @Steven...yup that's what I was asking for! – redconservatory Mar 9 '12 at 14:25
2

Wordpress has the following post types by default, with your custom post types add at the end of the array:

Array
(
    [0] => post
    [1] => page
    [2] => attachment
    [3] => revision
    [4] => nav_menu_item
    [5] => ...all your custom post types
)

So you can loop through them and simply add a meta box to every post type. Note: This won't work for attachment, revision and nav_menu_item, so you want to skip them:

function wpse44962_add_meta_boxes()
{
    foreach ( array_keys( $GLOBALS['wp_post_types'] ) as $post_type )
    {
        // Skip:
        if ( in_array( $post_type, array( 'attachment', 'revision', 'nav_menu_item' ) ) )
            continue;

        // You'll have to set $id, $title, $callback yourself:
        add_meta_box( $id, $title, $callback, $post_type, 'advanced', 'default' );
    }
}
add_action( 'add_meta_boxes', 'wpse44962_add_meta_boxes' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.