1

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?

  • 1
    I do not understand, what's the harm if you just use update_post_meta()? It will create the meta if it does not exist or update current one automatically. Am I missing something? – Ahmed Fouad Sep 21 '16 at 23:40
  • @AhmedMahdi probably the issue is just that, (add_post_meta instead of update_post_meta) you should post that as an answer... – gmazzap Sep 21 '16 at 23:49
3

It looks like you need to use update_post_meta()

https://codex.wordpress.org/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.

  • So I may be confused I guess, wouldn't update_post_meta update the value of that key even if it was different? For example, if I have a part_fits field with a value of 123 and another with a value of 1234, and if I used update_post_meta($id, 'part_fits', $value), wouldn't it not overwrite all of the meta_fields with that key? I will have multiple 'part_fits' on each post. – Lance Sep 22 '16 at 1:00
  • If you want to read more about the difference of both functions please check the wordpress codex. are you trying to store a unique (or non-unique) meta values for that key? – Ahmed Fouad Sep 22 '16 at 1:25
  • non-unique. So for example I have 5 posts, each of those post I add the id of 123 to it. I want those 5 posts to add their ids to the single post(123), but only if it is not already added. Currently it adds it either way. – Lance Sep 22 '16 at 1:40
  • $value = get_post_meta( $id, 'key', true ); if value is not empty so that metakey already exists – Ahmed Fouad Sep 22 '16 at 3:20
0

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.

0

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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