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.

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);
}
share|improve this question
add comment

1 Answer 1

I've added correct indenting to your question, which should make the problem more obvious.

The problem is that you meant:

foreach ( $state as $state )
    update_post_meta($post_id,'state',$state->name);

What you actually put is:

foreach ( $state as $state );
update_post_meta($post_id,'state',$state->name);

Which expands to:

foreach ( $state as $state ) {
    // do nothing
}

//insert post meta
update_post_meta($post_id,'state',$state->name);

A foreach loop will execute what immediatley comes after it. Here an empty statement is what comes after the foreach.

To prevent this happening again:

  • Indent your code correctly
  • Always use braces { } on your for/if/while/do/foreach loops/conditionals

Code editors such as sublime text or Komodo can re-indent code for you quite easily at a click of a button or a command

Here is what your foreach loop should look like:

foreach ( $state as $state ) {
    update_post_meta($post_id,'state',$state->name);
}

edit

I've also noticed another mistake that I really should have picked up on before

$state = get_terms('state', 'name');
foreach ( $state as $state ) {
    update_post_meta($post_id,'state',$state->name);
}

notice that you're using state each time? They shouldn't be the same. Instead do this:

$states = get_terms('state', 'name');
foreach ( $states as $state ) {
    update_post_meta($post_id,'state',$state->name);
}

foreach state S as state

Also you're not actually checking if the state is the one that you selected, so something like this:

$states = get_the_terms('state', 'name');
if ( $states && ! is_wp_error( $states ) ){
    foreach ( $states as $state ) {
        if($state->name == $_POST['yourinputsnamehere']){
            update_post_meta($post_id,'state',$state->name);
            break;
        }
}
}
share|improve this answer
    
Thanks for the help. I did make that change but the problem is the result is still the same as before. It gets a state name, but the incorrect one. It seems like I am missing something when it comes to the query and getting that specific records state. –  Dan Sep 20 '12 at 21:55
    
saw another problem, see revised answer –  Tom J Nowell Sep 21 '12 at 9:16
    
hmmm this code now adds each term in the states taxonomy to your post, but it doesn't include just the specific selected state, hmmm your question is missing bits, like the code for your form input –  Tom J Nowell Sep 21 '12 at 9:18
    
I actually found another issue. I was using get_terms. I changed it to get_the_terms. $state = get_the_terms($id, 'state', 'name'); This works now, but only on existing posts. When I create a new post there is no taxonomy initially set so I get an error because of the state as state. –  Dan Sep 21 '12 at 9:28
    
which is why you should always check values before iterating to see if theyre empty or error objects, updated code –  Tom J Nowell Sep 21 '12 at 9:57
show 1 more comment

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.