Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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
}
share|improve this answer
    
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;

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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