How to Display Custom Meta Box only on Specific Page IDs

I am using Magic Fields 2 for custom meta boxes and custom fields, but this situation pertains to any meta box.

I have a custom meta box that I want to display on only 3 specific page IDs.

Read More
function mf_1_remove_meta_boxes() {

    if( !in_array($_GET['post'], array('194','185','2') ) ):
        remove_meta_box( 'mf_1', 'page', 'normal' );
}

UPDATE

Using Bainternet’s solution, the code works. That being said, a new issue became apparent. When a new page is created, the meta box is visible immediately, then disappears once the page is saved, since the ID does not match an ID that has been specified.

UPDATE 2

The code above has been modified to correct the issue with the meta box appearing on a new (unsaved) page. It has also been modified to a complete and working code. The meta box will be removed for all users.

To remove the meta box for everyone except admins:

function mf_1_remove_meta_boxes() {

    if( !is_admin())
        return;

    if( !in_array($_GET['post'], array('194','185','2') ) ):
        remove_meta_box( 'mf_1', 'page', 'normal' );
}

Related posts

2 comments

  1. A simple and cleaner solution would be to use !in_array ex:

    function mf_1_remove_meta_boxes() {
    
        if( !is_admin() && !isset( $_GET['post'] ) )
            return;
    
        if( !in_array($_GET['post'], array('194','185','2') ) )
            remove_meta_box( 'mf_1', 'page', 'normal' );
    }
    

    this way you can just add the ids in the array and as many as you want

  2. Try:

    if($_GET['post'] != 194 || $_GET['post'] != 185 || $_GET['post'] != 2)

    It should work like this.

Comments are closed.