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.


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...

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

Full example...

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
  • Awesome, one question, in what format is the $_POST['tax_input']? Would it be slug, name, id? – AlxVallejo Nov 26 '12 at 23:41
  • It's going to be an array of the various tax_inputs -- just a standard array: numeric keys, string values. On a regular post probably something like array('category', 'format', 'post_tag'). – chrisguitarguy Nov 26 '12 at 23:45
  • And don't forget to stripslashes_deep() the $_POST values. – kaiser Nov 27 '12 at 5:21
  • @chrisguitarguy Wouldn't this be a multidimensional array? Within category for example, you'd have any selected category terms. What format would those terms be? slug, name, id? I'm trying to figure out how I can test this. – AlxVallejo Nov 27 '12 at 14:30
  • Yep, multi-dimensional array. The terms would likely be term ID's -- the quickest way to figure out would be to inspect element on those form fields. What is the value? – chrisguitarguy Nov 27 '12 at 15:08
$formats = $_POST['tax_input[formats][]'];


A var_dump( $_POST ); could be helpfull

'tax_input' => 
    array (size=1)
      'post_tag' => string 'tag_a,tag_b,tag_c' (length=...)

Try this:

add_action( 'save_post', 'find_post_tax' );

function find_post_tax(){

  $tax_to_fetch = 'supersonicscrewdriver';
  $found = false;

  if( isset( $_POST['tax_input']['formats'] ) && ! empty( $_POST['tax_input']['formats'] ) ){
    $taxs = explode( ',', $_POST['tax_input']['formats'] );
    $found = in_array( $tax_to_fetch, $taxs );

  return $found;

  • Works just expected but I used foreach() to make an array to check the found. $taxs=[]; $post_array = $_POST['tax_input']['formats']; foreach( $post_array as $array_id ) { $taxs[] = $array_id; } – omukiguy Jul 28 '20 at 19:24

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.