[Resolved] Insert multiple meta key – meta value pairs at once on post creation

Home Forums Support [Resolved] Insert multiple meta key – meta value pairs at once on post creation

Home Forums Support Insert multiple meta key – meta value pairs at once on post creation

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #906760
    George

    Hi Tom, I hope you won’t hate me for it as it’s slightly out of topic.

    The code found here creates a metakey with the value of 100 in the database whenever a new post is created.

    How can I modify the code so that another pair is also inserted at the same time using the same metakey (let’s say number 50)?

    I imagine that I need to put the values in an array and do a for loop or something but I can’t get my head around it. Can you maybe help? It shouldn’t take you long. hopefully!

    Cheers!

    #907043
    George

    Doh. Never mind, I got it working. Seems like inserting another add_post_meta line but removing the true argument did the trick.

    //Set Default Meta Value
    function set_default_meta_new_post($post_ID){
    
    if ($current_field_value == '' && !wp_is_post_revision($post_ID)){
        add_post_meta($post_ID,'groups-read','2');
        add_post_meta($post_ID,'groups-read','4');
    }
    return $post_ID;
    }
    
    add_action('wp_insert_post','set_default_meta_new_post');

    Cheers

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.