Home Forums Backend Issues (wp-admin) Check if value already exist 2 Reply To: Check if value already exist 2

  • Hey, I answered that question and I’ve recently run into the same issue in my own code. The problem is that the global $post does not have a value and there isn’t any way to get the current post id during the ajax call that is validating the field value.

    This is a bit complicated.

    The first thing that I did was to create a custom field named hidden_post_id, then I created an acf/load_value to add the current post id of the post being edited

    add_filter('acf/load_value/name=hidden_post_id', 'add_post_id_as_value'), 10, 3);
    function add_post_id_as_value($value, $post_id, $field) {
      return $post_id;

    The next step was to actually hide this field from the person editing the post. I did this by adding some custom admin CSS

    add_action('admin_head', array($this, 'hide_post_id_field'));
    function hide_post_id_field() {
        <style type="text/css">
          .acf-fields div[data-name="hidden_post_id"] {
            display: none;

    Then you need to alter your validation function to get this value an use it instead of using $post->ID

    add_filter('acf/validate_value/name=lien_video', 'validate_lien_video_filter', 10, 4);
    function validate_lien_video_filter($valid, $value, $field, $input) {
    	$post_id_field_key = 'field_568ad9d81e788';
      if (!$valid || $value == '' || !isset($_POST['acf'][$post_id_field_key])) {
        return $valid;
    	// change the field key below to the field key for your 
    	// hide_post_id_field field
    	$post_id = $_POST['acf'][$post_id_field_key];
      global $post; 
      $args = array(
        'post_type' => 'videos',  // or your post
        'post__not_in' => array($post_id), // do not check this post
        'meta_query' => array(
            'key' => 'lien_video',
            'value' => $value
      $query = new WP_Query($args);
      if (count($query->posts)) {
        // found at least one post that
        // already has $value
        $valid = 'There is already a post using this video';
      return $valid;