Support

Account

Home Forums General Issues custom post type ui with acf field with unique value Reply To: custom post type ui with acf field with unique value

  • hi
    i have a custom post type “professor” and a relationship field ( refer to “person” custom post type ) with name of “persona” but this code did not work

    
    
    add_filter('acf/validate_value/name=persona', '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
     
      // query existing posts for matching value
      $args = array(
        'post_type' => 'professor',
        'posts_per_page' => 1, // only need to see if there is 1
    
        'meta_query' => array(
          array(
            'key' => 'persona',
            'value' => $value
          )
        )
      );
      $query = new WP_Query($args);
      if (count($query->posts)){
        $valid = 'This Value is not Unique';
      }
    
      return $valid;
    }