Support

Account

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

Solved

Check if value already exist 2

  • Hi guys, I had started a topic there :
    http://support.advancedcustomfields.com/forums/topic/check-if-value-already-exist/

    About checking if a post with the same customfield value already exist :

    “I have created a custom field for video URL, what I would like to do is when the user type the URL, the URL is checked in the database and if there is already a post using it, it add a little message underneath saying there is already a post with this video.”

    Someone answers and it seems to working great except that It also display the error ‘There is already a post using this video’ when I just want to “Update” a post who already exist…

    Here is the code :

    <?php 
    add_filter('acf/validate_value/name=lien_video', 'validate_lien_video_filter', 10, 4);
    
    function validate_lien_video_filter($valid, $value, $field, $input) {
      if (!$valid || $value == '') {
        return $valid;
      }
      // query posts for the same value
      // for more info see
      // http://codex.wordpress.org/Class_Reference/WP_Query
      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;
    }
    
    ?>

    Thanks for your time.

  • 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;
          }
        </style>
      <?php 
    }
    

    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(
          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;
    }
    
  • 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;
    }
    
    
  • It’s a syntax error from a typo, I think it’s here,remove the extra ) in the second line you posted.

    
    add_filter('acf/load_value/name=hidden_post_id', 'add_post_id_as_value', 10, 3);
    
  • It worked great ! Thanks a lot again!

  • That’s exactly what I’m looking for – thanks.

    But is it possible to combine two or more fields?

    So if Field A has value ‘x’ and Field B value ‘y’, there must not be another combination of these two fields with these values.

    My code looks like this, but it doesn’t work:

    add_filter('acf/validate_value/name=strasse', 'validate_lien_video_filter', 10, 4);
    function validate_lien_video_filter($valid, $value, $field, $input) {
        $post_id_field_key = 'field_58ac5c77d3d6e';
      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; 
      $id = $post->ID;
      $plz = get_field('plz', $id);
      $location = get_field('ort', $id);
      $args = array(
        'post__not_in' => array($post_id), // do not check this post
        'meta_query' => array(
          'relation' => 'AND',
          array(
            'key' => 'strasse',
            'value' => $value
          ),
          array(
            'key' => 'plz',
            'value' => $plz
          ),
          array(
            'key' => 'ort',
            'value' => $location
          )
        )
      );
      $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;
    }

    Do you have a hint for me to solve this problem? Thank you so much! Really great support!

  • Since commenting on this I’ve also found that ACF does pass the current post ID along with other values during the AJAX request. What you need to do is check in the $_POST array in your filter. Not only does it contain the post ID, but it also has all of the values for all of the other fields. So there is really was never any reason for the extra work, I just never thought about looking at what else was already available.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Check if value already exist 2’ is closed to new replies.