Home Forums Backend Issues (wp-admin) Restrict meta value to only be assigned once Reply To: Restrict meta value to only be assigned once

  • Running into a brick wall trying to get the code working at the level you outlined in the original topic. (For it to validate across the whole post type).

    I have created a test field called ‘Validation Test’ with the name ‘test’.
    I am able to get the post ID to auto generate as you outlined. I have inspected the field to obtain its field key, to which I have then referenced in the necessary places in the code.

    I have also made sure that the filters are referencing the correct field i.e.

    load_value is using the field name=hidden_post_id (post id field)


    validate_value is using name=test (validation test field name)

    Scratching my head as to why it won’t valid, I am continuing to try and prod around to get to the bottom of this.

    Is there anything obvious in my code that you can see? (I left out the ‘hide field’ code snippet you outlined in the original topic)

    //Auto fill in post id 
    add_filter('acf/load_field/name=hidden_post_id', 'make_hidden_post_id_readonly');
    function make_field_readonly($field) {
      // sets readonly attribute for field
      $field['readonly'] = 1;
      return field;
    add_filter('acf/load_value/name=hidden_post_id', 'set_hidden_post_id_value', 10, 3);
    function set_hidden_post_id_value($value, $post_id, $field) {
      // set the value of the field to the current post id
      return $post_id;
    //Validate field 
      add_filter('acf/validate_value/name=test', 'require_unique', 10, 4);
    function require_unique($valid, $value, $field, $input) {
      if (!$valid) {
        return $valid;
      // get the post id
      // using field key of post id field
      $post_id = $_POST['acf']['field_57765d437d860'];
      // query existing posts for matching value
      $args = array(
        'post_type' => 'knowledge-base',
        'posts_per_page' => 1, // only need to see if there is 1
        'post_status' => 'publish, draft, trash',
        'post__not_in' => array($post_id),
        'meta_query' => array(
            'key' => $field['test'],
            'value' => $value
      $query = new WP_Query($args);
      if (count($query->posts)){
        $valid = 'This Value is not Unique';
      return $valid;