Support

Account

Home Forums General Issues Check if value already exist by filtering post object

Unread

Check if value already exist by filtering post object

  • Hi everyone, I have a post object field and a number field in “post” post type, and i want to check if value already exists in number field in posts that have the same value in post object field,

    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; 
      $arg = array(
    	  'post_type' => 'post',
    	  'meta_key' => 'chapter_num',
    	  'meta_query' => array(
    	  	'key' => 'manga_title',
    	  	'value' => '"'.get_the_ID().'"',
    		'compare' => '!=',
    	  ));
      $args = array(
        'post_type' => 'post',  // or your post
        'posts__not_in' => array($arg), // do not check this post
        'meta_query' => array( 
          array(
            'key' => 'chapter_num',
            'value' => $value
          )
        )
      );
      $query = new WP_Query($args);
      if (count($query->posts)) {
        // found at least one post that
        // already has $value
        $valid = 'there is a post have the same number!';
      }
      return $valid;
    }

    this is my code, but it is not working.
    is it possible to do what I want?

Viewing 1 post (of 1 total)

The topic ‘Check if value already exist by filtering post object’ is closed to new replies.