Support

Account

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

  • Thanks for your answer !

    I tried to use your code but it display an error and I can’t find why :
    Parse error: syntax error, unexpected ‘,’ in /home/guillaummg/www/faview/wp/wp-content/themes/ffaview/functions.php on line 209

    add_filter( 'posts_distinct', 'cf_search_distinct' );
    
    // PREVENT POST THAT ALREADY EXIST
    add_filter('acf/validate_value/name=lien_video', 'validate_lien_video_filter', 10, 4);
    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;
    }
    function validate_lien_video_filter($valid, $value, $field, $input) {
    	$post_id_field_key = 'field_569ce8b4cba44';
      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(
          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;
    }