How to update post meta data with multiple values

1k Views Asked by At

I have an array I am looping through and each loop I figured it would either update or add to the database so I could have multiple product IDs in the postmeta table but it only adds the last one and overrides all the other values.

Is there a way to loop through and add each one without the previous loops being overridden? I have tried both update_ and add_ post_meta both with the same result.

    $number_products = get_price_cart_count();
    $productIDsInCart = get_product_ids();

    $throwback_ids = get_post_meta( '13376', 'product_ids', true );
    $throwbackProducts = explode( ',', $throwback_ids );


    $result = array_intersect($throwbackProducts, $productIDsInCart);

    foreach($result as $res){


            add_post_meta( '683934', 'exclude_product_ids',$res);

    }
1

There are 1 best solutions below

0
On

Hmm this seemed to do the trick. No looping just imploding the data and pushing them all in at once.

    $number_products = get_price_cart_count();
    $productIDsInCart = get_product_ids();

    $throwback_ids = get_post_meta( '13376', 'product_ids', true );
    $throwbackProducts = explode( ',', $throwback_ids );


    $result = array_intersect($throwbackProducts, $productIDsInCart);


    $data = implode( ',', $result);


    update_post_meta( '683934', 'exclude_product_ids',$data);