1

I'm having an issue saving post meta on all post types, both default and custom. I'm hooking in to save_post to run my function, and what's happening is that the post meta gets added, but then immediately deleted. This is what I have right now:

function saveSidebarMeta($postId) {
    $toAdd = $_POST['collection-topics'];
    foreach ($toAdd as $topic) {
        add_post_meta($postId, 'collection-topic', $topic);
    }
}

add_action('save_post', 'saveSidebarMeta', 100);

Right now, I have the priority set at 100, and that does save the post meta, but then if the page is saved again the post meta gets deleted unless the correct info is added and I don't want it to be deleting and adding the data every time someone saves a post.

If the priority is set to the default 10, the data does get saved, but then immediately deleted. I know this because 1.) add_post_meta() returns the meta_key, which is then absent from the database and 2.) if I put a die() statement right after add_post_meta(), the meta data shows in the table.

I have other functions saving post meta just fine. Am I missing something really simple here? I've been looking through this issue for a while and have run out of places to look or directions to go in for a solution. Any help would be appreciated. Thanks!

2
  • Could it be that it is adding your meta. But your meta is empty?
    – ngearing
    Jul 25, 2016 at 21:38
  • 1
    Can we see the form generating code? Do you need to store these as multiple single meta values, or could you store an array? Jul 26, 2016 at 9:00

2 Answers 2

1

Are you outputting the saved data to the edit post page before saving the post? If not you may be getting a empty $_POST['collection-topics'] data and then saving empty or null data.

I would do a check before doing add_post_meta()

if ( empty($_POST['collection-topics']) ) return;
2
  • 1
    But that makes it impossible for a user to delete the field content! Instead all the custom fields should have their values populated when the form is displayed. Jul 25, 2016 at 22:34
  • You have to do that when you display the custom field. Use echo get_post_meta('Bar') for the value needed
    – stoi2m1
    Jul 26, 2016 at 1:16
0

Here's a start, but we also need to see the code that generates the form.

One possibility is that auto-save is messing you up, so try this variation on your code.

add_action('save_post', 'saveSidebarMeta', 100);

function saveSidebarMeta( $postId ) {

    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
        return $post_id;
    } else {    

        $toAdd = $_POST['collection-topics'];
        foreach ($toAdd as $topic) {
            add_post_meta($postId, 'collection-topic', $topic);
        }
    }

}

But if we can see your form generating code too I might be able to expand this further.

Your Answer

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

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