0

I've got more than a few metaboxes being used with a custom post type and I'm running into an issue where my data is not being saved as expected.

Here is the relevant part of the code, and its within a function hooked to save_post.

// Loop through our custom metabox post data
foreach ($_POST['product'] as $key => $value) {
    // Clear the previous featured product if it needed
    if ($key == 'homefeature' && $value == 'on') {
        $wpdb->query(
            "
            UPDATE      $wpdb->postmeta
            SET         meta_value = 'off'
            WHERE       meta_key = 'the_prefix_homefeature'
                        AND meta_value = 'on'
            "
        );
    }

    // Update meta
    update_post_meta($post_ID, 'the_prefix_' . $key, $value);
}

The query seems to work just fine (I've tested it outside of Wordpress) but in the end the product will not be flagged as the homefeature. The weirdest thing is that if I put a die in after the above loop finishes, it seems to work as expected.

It almost seems like Wordpress is caching the query or something. Any help figuring out why this is happening would be great, and let me know if you need more info/code.

3
  • I don't quite understand what you're wishing to accomplish, but shouldn't you add AND post_id = <ID of targeted post> to the SQL query? Otherwise that query will just toggle all meta_values that are off to on, for all posts. Is that what you intend?
    – Simon
    Jan 30, 2013 at 18:23
  • Well, I'm toggling from on to off (not the other way around) but yes, that's what I'm trying to do. There can be only one featured product, so I have to go and flag the current one as off.
    – The Maniac
    Jan 30, 2013 at 18:34
  • I see, that make sense.
    – Simon
    Jan 30, 2013 at 20:17

1 Answer 1

0

I know save_post is called twice when a post is saved. The fact that die() solves your problem indicates that the second call is undoing the first, perhaps?

save_post will also be called on autosave, so you might want to eliminate those too.

4
  • I am handling the autosave, but if its called twice then maybe I can solve my problem by unsetting some POST variables. I'll give it a try.
    – The Maniac
    Jan 30, 2013 at 19:11
  • So unsetting $_POST['product']['homefeature'] fixed it! I'm still confused at why this would happen, because even if save_post is called twice it should work.
    – The Maniac
    Jan 30, 2013 at 19:15
  • I'm not entirely sure, but I suspect that $post_ID is different on the second pass, so the delete happens but updating the post meta doesn't. You could test this by outputting it for each call I guess.
    – vancoder
    Jan 30, 2013 at 19:35
  • @TheManiac, save_post fires twice on Publish and one of them is a revision, checking for 'revision' == $post_object->post_type catches it. Not sure if this applies to your case, though...
    – brasofilo
    Jan 30, 2013 at 22:13

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.