1

I have a custom post type called "listing", and a custom taxonomy called "listing_state".

I'm using formidable to create posts. What I am trying to do is have two things happen when a user selects a state from the taxonomy drop down.

First, to set the taxonomy, which is what happens now. But then at the same time, also have this selected state get copied to a custom field called "state".

The end result should be a listing that is classified by state, but that also holds that state name in a custom field as well.

Any ideas? Thanks in advance for any help.

0

1 Answer 1

0

Try hooking into 'save_post' action:

add_action( 'save_post', 'your_state_term_save' );

function your_state_term_save( $post_id ){

    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    //check if they selected your state term
    $state = isset($_POST['tax_input_field_name']) ? $_POST['tax_input_field_name'] : ''; //make sure of what the input name is here...

    //insert post meta
    update_post_meta($post_id,'state',$state);
}

This works by waiting till the post is saved and grabbing the taxonomy term right out of the $_POST vairable.

3
  • Hi I tried but it doesn't work. What do you mean by the field name? My taxonomy is state and the fields it holds are ID, Name and Slug. I would like to copy the name. Thanks
    – Dan
    Commented Sep 15, 2012 at 4:45
  • Because you had some kind of custom dropdown box for selecting the state, I was not sure what the 'name' attribute of your select box was. (The 'name' attribute is the key that will be stored in the $_POST variable. You need to look at the html and change out tax_input_field_name with the correct field name in your taxonomy meta box. Commented Sep 17, 2012 at 18:05
  • 1
    I was not able to get it to work. But I did try my own solution and it's almost there. Everything works except that the incorrect state is being inserted. PS I'm doing the updates/saves through the WP Admin. Can you see what the issue is? Thanks add_action( 'save_post', 'add_state' ); function add_state( $post_id ){ // Bail if we're doing an auto save if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; //check if they selected your state term $state = get_terms('state', 'name'); foreach ( $state as $state ); //insert post meta update_post_meta($post_id,'state',$state->name); }
    – Dan
    Commented Sep 20, 2012 at 10:23

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.