Support

Account

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

Solved

Check if value already exist

  • Hi everyone, I’m new using ACF and PHP, I have readed the doc but can’t seems to find what i’m looking for.

    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.

    Thanks a lot for your time.

  • This could be done, but not directly, it will take a little work. What you’d need to do is set up an acf/validate_value filter outlined here http://www.advancedcustomfields.com/resources/acf-validate_value/

    In the filter you can then do a query of all of the posts to see if there is another post with the same field value and return an error. Something along the lines of:

    
    add_filter('acf/validate_value/name=field_name', 
                'validate_field_name_filter, 10, 4)
    function validate_field_name_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' => 'post',  // or your post
        'post__not_in' => array($post->ID), // do not check this post
        'meta_query' => array(
          array(
            'key' => 'field_name',
            '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 worked great ! Thanks a lot !
    Here is my code if it can help someone else :

    The name of my field is : lien_video
    Name of my custom posts : videos

    <?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;
    }
    
    ?>
  • @warry
    your code helps me a lot!
    it works very well when user tries to add (publish) new post with same value, it gives him the error message that I wrote, the problem happens when the user tries to update other fields at the same post and save, this gives a duplicate error (but in fact it’s not duplicated, the value is exist in only current post, not in the others)

    so, I tried to use get_the_id(); function to ignore current post ID from being checked, but it doesnt work, my code as follows, my field named as “important_estate_clients_mobile” and CPT named “important_clients”:

    // check if mobile field value is already exist
    add_filter('acf/validate_value/name=important_estate_clients_mobile', 'validate_important_clients', 10, 4);
    
    function validate_important_clients($valid, $value, $field, $input) {
      if (!$valid || $value == '') {
        return $valid;
      }
      // query posts for the same value
      // http://codex.wordpress.org/Class_Reference/WP_Query
      global $post; 
       $current_post_id = get_the_id();
      $args = array(
        'post_type' => 'important_clients',  // or your post
        'post__not_in' => array($current_post_id), // do not check this post
        'meta_query' => array(
          array(
            'key' => 'important_estate_clients_mobile',
            'value' => $value
          )
        )
      );
      $query = new WP_Query($args);
      if (count($query->posts)) {
        // found at least one post that
        // already has $value
        $valid = 'رقم العميل هذا مدخل من قبل';
      }
      return $valid;
    } 
Viewing 4 posts - 1 through 4 (of 4 total)

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