I have a taxonomy called state which lists all the states. When I create a post I want to select the taxonomy, but also have the name of the state automatically captured in a custom field as well.
I've manage to get this code working, however I'm having trouble getting the correct value from that post. A state name is being copied but it's from a different record.
I know it has to do with the foreach, but when I put the postid it does not work.
Also, it should work whether I do it through the admin, or via the front end in a form that submits or edits the post.
Here is the code, any help is appreciated.
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);
}