0

Is there any way to use get_post_meta within a function called by the add_meta_boxes action hook?

I only want to display the meta box if a specific post meta matches a regex, but so far, I have not idea how to do so.

0

You can not put condition in callback function as it will display the meta box without the HTML. Better you can wrap add_meta_box() in if condition.

If you are on post edit screen you can get the post id in this way.

$post_id = false;
if (!empty($_POST['post_ID'])) {
    $post_id = $_POST['post_ID'];
} else if (!empty($_GET['post'])) {
    $post_id = $_GET['post'];
}

if (get_post_meta($post_id, 'yourKey', true)) {
    add_meta_box($id, $title, $callback, $screen, $context, $priority, $callback_args);
}
  • $_GET['post'] instead global $post worked, thank you. – petermolnar Apr 22 '15 at 11:53

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.