Support

Account

Home Forums General Issues Meta query for image field issue

Solved

Meta query for image field issue

  • Hello friends,

    I want to perform a query to get all posts who have the acf image field ‘image_delight’ set. So I tried this:

    $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' => '!=',
    		),
    	),
    
    ) );

    The problem I face is with posts which had a ‘delight_image’ defined once yet it was removed in the post admin interface again. So while the EXISTS meta query correctly only selects the posts which have or had a ‘delight_image’ defined once the second part of the meta query doesn’t seem to exclude the ones whose ‘delight_image’ was removed again. Any suggestion how to get this done?

    Thanks

    martin

    update:
    I need this type of query for a paged query, so I cant do the check if the field has a valid entry after the query.

  • Try this

    
    $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' => array(''),
    			'compare' => 'NOT IN',
    		),
    	),
    
    ) );
    

    It’s from something I found here http://wordpress.stackexchange.com/questions/10881/how-can-i-show-posts-only-if-meta-value-is-not-empty

  • Hi John, thanks for the reply, I found that stackexchange answer too yet it doesnt seem to work for the acf image field. Question is what the acf plugin does when you remove an image from the image field in the post edit field (by pressing the cross).

  • 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);
    
  • Thanks again for the reply. Unfortunately this solution also doesnt seem to work for me. Where can i find the actual database entry to double check how the field is set? I am just off now soon so will check again tomorrow.

  • The value is in the postmeta table, the meta_key is your field name… unless it’s a repeater sub field.

    You should also be checking to see what the actual query being run is. I don’t see anything in that should be stopping this from working, though I have not set up a way to test it on my own.

  • Thanks again for keep supporting me here, particular as this is clearly a wp issue as I understand now.

    Since I had to come up with a quick solution I just decided to go for the following workaround:

    When I import a new image I set the value of the image field explicitly to ”. (Which is very doable in my case as I create all posts via wpcli …)

    Since there is no need anymore to check for cases of non existent image fields I just query as follows (which is the complete query as i use it in my actual program, including only an additional paged parameter)

    $wp_query = new WP_Query( array(
    	'post_type'      => 'post',
    	'posts_per_page' => 15,
    	'paged'          => $paged,
    	'meta_key' => 'delight_image',
    	'meta_query' => array(
    		array(
    			'key' => 'delight_image',
    			'value' => '',
    			'compare' => '!=',
    		),
    	),
    ) );

    Luckily it now works as expected. Sorry I didn’t dig deeper into this but sometimes workarounds are just the way to go 🙂

    thanks

    maritn

  • It’s not really a problem. I’m sure that others will run into a similar issue and hopefully they’ll find this thread in there search for an answer. Honestly, it’s a fairly good work around. If a post is added in the admin it will always have a value even it that value is nothing.

  • Oh thanks for that note, i was wondering before why I have to check on existence anyway yet now I understand as you pointed out that this is related to me working with wpcli created posts. Then I should set the field values explicitly to a value anyway when working with wpcli in order to have things consistent in the database. Thanks again

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

The topic ‘Meta query for image field issue’ is closed to new replies.