I have a custom field on a WooCommerce product, and I am entering the ID of another product into it. When saving that product, I am adding a meta field to the product that was inputted, creating a "link" between them. I have this working fine, but the issue is that it adds it even it is already there.
function part_fits($post_id){
global $post;
global $product;
$current_diagram_id = get_the_ID();
if( have_rows('product_association') ):
while( have_rows('product_association') ): the_row();
$single_part_id = get_sub_field('part');
add_post_meta($single_part_id, 'part_fits', $current_diagram_id);
endwhile;
endif;
}
Is there a way I can check if that exact key and value already exists and only add it if it does not?
I have a custom field on a WooCommerce product, and I am entering the ID of another product into it. When saving that product, I am adding a meta field to the product that was inputted, creating a "link" between them. I have this working fine, but the issue is that it adds it even it is already there.
function part_fits($post_id){
global $post;
global $product;
$current_diagram_id = get_the_ID();
if( have_rows('product_association') ):
while( have_rows('product_association') ): the_row();
$single_part_id = get_sub_field('part');
add_post_meta($single_part_id, 'part_fits', $current_diagram_id);
endwhile;
endif;
}
Is there a way I can check if that exact key and value already exists and only add it if it does not?
Share Improve this question asked Sep 21, 2016 at 23:26 LanceLance 231 gold badge1 silver badge3 bronze badges 2 |4 Answers
Reset to default 4It looks like you need to use update_post_meta()
https://codex.wordpress/Function_Reference/update_post_meta
Source: WP Codex
The function update_post_meta() updates the value of an existing meta key (custom field) for the specified post.
This may be used in place of add_post_meta() function. The first thing this function will do is make sure that $meta_key already exists on $post_id. If it does not, add_post_meta($post_id, $meta_key, $meta_value) is called instead and its result is returned.
Returns meta_id if the meta doesn't exist, otherwise returns true on success and false on failure. It also returns false if the value submitted is the same as the value that is already in the database.
add_post_meta()
has an optional fourth parameter $unique
– when this is set to true
, the custom field will not be added if the given key already exists among custom fields of the specified post.
The function will return false
if the $unique
argument was set to true
and a custom field with the given key already exists.
I would use the $product->meta_exists function, like this:
if( !$product->meta_exists( $key ) ) {
if( $new_val !== $existing_val ) {
$product->update_meta_data( $key, $new_val );
}
}
p.s. Don't forget to save changes:
$product->save_meta_data();
That one sentence:
- "It also returns false if the value submitted is the same as the value that is already in the database."
...is incredibly important (thanks Ahmed Fouad), because it is entirely counter-intuitive! You would reasonably expect an update of the database to return success (true) when the action is not a genuine failure. But here Wordpress gives failure (false) when the data is unchanged. This had me fooled completely, because it is NOT mentioned in the codex, except in the contributed notes!
Would you not expect "failure" to mean failure to reach the database?
As a result I am using a short function to wrap up the problem:
function check_update_post_meta( $postid, $key, $value ) {
// Get the single/first value of this key in this post's meta
$response = get_post_meta( $postid, $key, true );
if ($response == $value) {
// If the value is already set return true
return true;
} else {
// Replace the old value with the new, or add a key into the db
$response = update_post_meta( $postid, $key, $value );
}
return $response;
// Now 'false' means a failure to reach the database
// Now 'true' means the data now exists in the database without or with changes
// A return int>0 means a new record with this ID was added
// Note: It's not possible that the ID=1 when calling this function from within a post.
}
add_post_meta
instead ofupdate_post_meta
) you should post that as an answer... – gmazzap Commented Sep 21, 2016 at 23:49