简体   繁体   中英

add_post_meta Multi value not working when i try to add them using loop

At WordPress, I use a loop to add multiple meta values at a meta field. But every time it adds the lat value.

Here is my code:

$value = array("red", "green", "yellow")
foreach ( $value as $item ) {
    add_post_meta( $post_id, "_color", $item );
}

Every time after saving it save only yellow.

NOTE: This code works on my server but when I try to a client-server, it is behaving like this.

Wordpress Version: 4.7

Please help me.

Lets try by adding fourth parameter as "false".

add_post_meta( $post_id, "_color", $item, false );

I know, the fourth parameter is 'false' by default, but lets try it once, because your value is overwritten every time by the same key, therefore it is storing the last value ie "Yellow".

check this code:

$post_id = $post->ID;
add_post_meta($post_id, 'counter_meta', 'XYZ');
$return = get_post_meta($post_id, 'counter_meta', true );
echo '<pre>';
print_r($return);
exit();

Please try this

$post_id = 12;
$value = array("red", "green", "yellow")
foreach ( $value as $item ) {
    add_post_meta( $post_id, "_color", $item );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM