admin管理员组

文章数量:1430624

I have a custom field called "website_url". It should be unique.

Ex: If a author set "" as the custom field value as a post, "" can't be used any other post.

How to do it?

I have a custom field called "website_url". It should be unique.

Ex: If a author set "https://wordpress.stackexchange/questions/ask" as the custom field value as a post, "https://wordpress.stackexchange/questions/ask" can't be used any other post.

How to do it?

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Jul 14, 2014 at 13:30 DamithDamith 212 bronze badges 2
  • This may be a bit much but an idea could be to create a custom taxonomy which isn't used anywhere but the admin panel. Store each 'url' as a term and then you can use term_exists() to check if that url exists. Though that means you could have variations of the same url (ie. /ask and /ask/) which may not be what you want. Otherwise saving it as post_meta you would have to pull post meta for every post and run it against the current meta, which is resource intensive. Finally there's the custom DB Table route. Just my 2 cents... – Howdy_McGee Commented Jul 14, 2014 at 16:51
  • @gmazzap, code is working fine and it is not firing with ``` add_filter( 'update_post_metadata', 'my_unique_url_meta', 10, 4 ); ``` – Thamisetty Krishna Mohan Commented May 7, 2019 at 10:43
Add a comment  | 

2 Answers 2

Reset to default 2

You can use the filter hooks 'add_post_metadata' and 'update_post_meta', when a function hooked there return anything but NULL the process of adding/updating metadata is stopped.

The two hooks passes almost same args:

  1. NULL
  2. post id
  3. the meta key
  4. the meta value being added/updated

Only last argument is different, in case of update is previous value, in case of add is the $unique value passed to add_post_meta.

The idea is:

  1. run a filter callback for both hooks
  2. use a custom SQL query to be sure the value is unique
  3. id the value is already used, then return anything but NULL

Example:

add_filter( 'add_post_metadata', 'my_unique_url_meta', 10, 4 ); 
add_filter( 'update_post_metadata', 'my_unique_url_meta', 10, 4 );

function my_unique_url_meta( $null, $pid, $key, $value ) {
  // here I assume the meta key to force unique is "url", change this to fit your needs
  if ( $key !== 'url' ) return $null;
  global $wpdb;
  $exists = $wpdb->get_col( $wpdb->prepare(
    "SELECT meta_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s",
    $key, $value
  ) );
  // return anything but NULL stop the process, however returning FALSE is the only way
  // to obtain a "pretty" error message by WordPress in post admin screen
  if ( ! empty( $exists ) ) return FALSE;
}

Using previous code, when the meta field for the meta key already exits, you obtain an error message like "Please provide a custom field value." just like you haven't provided any custom field value, and I'm afraid that message can't be changed. In addition, if after added an already used meta value for the custom field, you straight save/update the post (before click on "Add Custom Field" button inside the metabox) than you'll see no error message, but when the page is reloaded the custom field is just not there.

Previuos warning only apply to core "Custom Fields" metabox, but you can use a custom metabox for your unique metakey and use ajax to show a proper error message, however, my code assure no one can add a non-unique meta value even using code (and not UI).

You can add custom error message with the following code

  if ( ! empty( $exists ) ) {
    $post_id  = get_post_meta_by_id($exists[0])->post_id;
    wp_die( __( $key.' - '. $value .' already exists for Post ID:'.$post_id));
    return FALSE;
  }

本文标签: WordPress Custom Field Should be Unique Is it possible