2

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

0

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
}
5
  • Awesome, one question, in what format is the $_POST['tax_input']? Would it be slug, name, id?
    – AlxVallejo
    Commented Nov 26, 2012 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'). Commented Nov 26, 2012 at 23:45
  • And don't forget to stripslashes_deep() the $_POST values.
    – kaiser
    Commented Nov 27, 2012 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
    Commented Nov 27, 2012 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? Commented Nov 27, 2012 at 15:08
2
$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;

}
1
  • 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
    Commented Jul 28, 2020 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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