2
votes

I need to add a post meta keeping and I want the value to be unique, but according to the add_post_meta() codex there is only the option to set the key as unique. Can I do the same with the value?

The only solution I found is to check the post meta doing a get_post_meta(), then search into the array for a duplicate value, but maybe it's too intricate.

1
a foreach? Very Overloaded. Why you dont check it directly as DB Query over $wpdb? Another solution,.. forget it. - Adrian Preuss
Which solution is faster, wpdb query or get_post_meta then an array search (without foreach)? Thanks for the advice :) - Flavio Li Volsi

1 Answers

2
votes

Check it manually over an Database query like:

 $search = "What you searching for?";
 if($wpdb->get_var($wpdb->prepare("SELECT COUNT(`meta_value`) FROM $wpdb->postmeta WHERE `meta_value`=%s", $search)) == 0) {
      // Dont exists,.. Add a new post meta for example
 }