I want to add Metabox for the posts of category=18 and i am using the following code but unable to do so . So please help me out->


function my_meta_init()
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;

    // checks for post/page ID
    if ($post_id->post_category[0] == 18)
        add_meta_box('team_meta', 'My Custom Meta Box 1', 'team_meta_1', 'post', 'normal', 'high');

    function team_meta_1(){
    global $post;
    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . 
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

    // Get the location data if its already been entered
    $designation = get_post_meta($post->ID, '_designation', true);

    // Echo out the field
    // echo '<input type="text" name="_description" value="' . $description  . '" />';
    echo '<textarea name=_designation rows="6" cols="100">'.$designation.'</textarea>';


    function my_meta_save($post_id, $post) {

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
    return $post->ID;

    // Is the user allowed to edit the post or page?
    if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;

    // OK, we're authenticated: we need to find and save the data
    // We'll put it into an array to make it easier to loop though.

    $events_meta['_designation'] = $_POST['_designation'];

    // Add values of $events_meta as custom fields

    foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
        if( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn't have a value
            add_post_meta($post->ID, $key, $value);
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank




1 Answer 1


Instead of if ($post_id->post_category[0] == 18) try

if ( $post_id && in_category( 18, $post_id ) )

Also the 'save_post' action should be

 add_action('save_post','my_meta_save', 10, 2);

If you want the metabox to appear on a new post when the category is selected, then remove the outer category test if statement so that the metabox is always added and then show/hide using jquery (put this after the echo of the textarea)

echo '<textarea name=_designation rows="6" cols="100">'.$designation.'</textarea>';
<script type="text/javascript">
jQuery(document).ready(function() {
    (function ($) {
        $('#in-category-18').change(function () { $('#team_meta').toggle(this.checked); }).change();
    – terminator
    Aug 1, 2014 at 7:15
  • Note the metabox will only show after you save the post in the appropriate category - also just noticed that the add_action('save_post','my_meta_save') is missing arguments, should be add_action('save_post','my_meta_save', 10, 2) - will update answer.
    – bonger
    Aug 1, 2014 at 7:34
  • Yup its working now , but what is the use of giving user a Metabox if its going to appear after i save the post. Rather it should appear as an when user click on category check box.
    – terminator
    Aug 1, 2014 at 7:53
  • Ok a jquery one-liner can to do that, after taking the adding of the metabox out of the if statement so it's always added but shown/hidden depending on category - I'll update the answer.
    – bonger
    Aug 1, 2014 at 8:32
  • waiting for the reply
    – terminator
    Aug 1, 2014 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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