Remove Meta Box if CPT cutom Meta Data exists
-
I have a CPT that each post can link to an associated User Account. This is done with a dropdown box that lists the user accounts and saves the user ID to the custom meta data field ‘attach_user_ID’
If the CPT has an associated user ID (user account) attached, it displays the associated information with that user account, email, phone and more.
This all works fine except that if the post doesn’t have an associated user account, I want to hide the meta box that displays that extended information.
Been banging my head on this one for a while.
code is as follows:
Add custom meta boxes:
add_action("admin_init", "admin_init"); //Add custom meta boxes and fields to CPT Lodge Members (officers) function admin_init(){ add_meta_box("assoc_info", "Associated Information", "assoc_info", "officers", "normal", "low"); }
Left out the code that adds the other meta boxes that store the user ID, but this works fine, and I can call the user ID ok from most areas of the code.
function remove_assoc_info_meta_box() { global $post; //Can't see attach_user_ID at this point, displays nothing. echo get_post_meta( $post->ID, "attach_user_ID", true); remove_meta_box('assoc_info', 'officers','normal'); } //if ( empty( get_post_meta( $post->ID, 'attach_user_ID' ) )) { add_action('admin_menu', 'remove_assoc_info_meta_box'); //}
the remove meta box code works, but I can’t check the custom meta data value at this point in the code.
If I move the add_action(remove meta box) code to a portion of the code where I can check the user ID, the meta box doesn’t hide.
any help greatly appreciated.
- The topic ‘Remove Meta Box if CPT cutom Meta Data exists’ is closed to new replies.