Support

Account

Home Forums General Issues Meta query for image field issue Reply To: Meta query for image field issue

  • When you remove the image and update the post acf sets the db value to ” and empty string. There does not seem to be any reason why the query you had should not work other than something in WP. It seems that by putting in the ‘EXISTS’ part that WP does not use the second part with an empty value. There is an answer before that one that covers altering the WHERE part of the query.

    
    function yoursite_posts_where($where,$query) {
      global $wpdb;
      $new_where = " TRIM(IFNULL({$wpdb->postmeta}.meta_value,''))<>'' ";
      if (empty($where))
        $where = $new_where;
      else
        $where = "{$where} AND {$new_where}";
      return $where;
    }
    add_filter('posts_where','yoursite_posts_where',10,2);
    $wp_query = new WP_Query( array(
    	'post_type'      => 'post',
    	'posts_per_page' => -1,
    	'meta_key' => 'delight_image',
    	'meta_query' => array(
    		'relation' => 'AND',
    		array(
    			'key' => 'delight_image',
    			'compare' => 'EXISTS',
    		),
    		array(
    			'key' => 'delight_image',
    			'value' => '',
    			'compare' => '!=',
    		),
    	),
    
    ) );
    remove_filter('posts_where','yoursite_posts_where',10,2);