get_terms on save_post hook

Usually, if I want to check for some attribute of the post I’m about to publish, I will check like this:

$post_title = $_POST['post_title'];

post_title is the name of the title field in the edit window.

I thought I’d apply the same logic to the taxonomy meta box.

$formats = $_POST['tax_input[formats][]'];

Essentially, I’m trying to check if a specific taxonomy term is being selected when I publish a post.

2 Answers
2

You’re on the right track.

When you do something like this in a form..

<input name="tax_input[formats][]" />

You create levels in the $_POST array.

So you’d get the formats array like this…

<?php
$formats = $_POST['tax_input']['formats'];

Full example…

<?php
add_action('save_post', 'wpse74017_save');
function wpse74017_save($post_id)
{
    // check nonces and capabilities here.

    // use a ternary statement to make sure the input is actually set
    // remember that `save_post` fires on every post save (any post type).
    $formats = isset($_POST['tax_input']['formats']) ? $_POST['tax_input']['formats'] : array();

     // do stuff with $formats
}

Leave a Comment