Support

Account

Home Forums General Issues Issue with meta_query and ACF

Solving

Issue with meta_query and ACF

  • I’m basically trying to loop through the custom post type “Projects” and find only projects with a homepage slide image selected.

    <ul class="slides">
    	    	<?php $args = array( 'post_type' => 'project', 'meta_query' => array('key' => 'homepage_slide','value' => 'true'));
    			$loop = new WP_Query( $args );
    			while ( $loop->have_posts() ) : $loop->the_post(); ?>
    	    	
    	    	<li>  
    	    		<img src="<?php the_field('homepage_slide'); ?>" alt="slide" /></li>
    			
    	    	<?php endwhile; ?>	    	
    	    </ul>
    

    Unfortunately this is still including projects that don’t have homepage slides attached. Any ideas?

  • Hi @cutout

    the same as your previous thread:
    http://support.advancedcustomfields.com/forums/topic/using-meta_value-with-query_posts/

    ‘key’ is not a valid WP_Query param.

    Please double check the docs and use meta_key

    Thanks
    E

  • Unfortunately when I change it, it still loops through all projects and displays a broken image, rather than only catching projects with slideshow images:

    $args = array( 'post_type' => 'project', 'meta_query' => array('meta_key' => 'homepage_slide','meta_value' => 'true'));

  • Hi @cutout

    looking at the docs (http://codex.wordpress.org/Class_Reference/WP_Query) i can see that the meta_query array param requires a key and value. Sorry, I did not see that you were using this as an array, but thought you were using the basic meta_key / meta_value params.

    The issue is most likely with the value true. WordPress only saves strings to the DB, not booleans. Perhaps if you change the value to ‘1’ it will work?

    Failing that, please download a plugin to debug your DB queries, then review the SQL produced to find what is wrong with the query.

    Thanks
    E

  • It’s odd, but this still doesn’t work –

    $args = array( 'post_type' => 'project', 'meta_query' => array('key' => 'homepage_slide', 'value' => '1'));

    I will check out a debug plugin and see if I see anything wrong.

    Just curious, if you were trying to do the same thing (add slides to a slideshow only if the homepage_slide field was completed), how would you write the query?

  • Hi @cutout

    I don’t think you have yet explained what type of field is ‘homepage_slide’.

    Am I right in assuming it is a gallery or repeater field?
    If so, the value will not be ‘1’.

    Perhaps you will need to use some of WP COMPARE query args to look for EXISTS or event ‘> 1’

    You may even find that removing the value param will allow WP to just find connections but not compare the actual value

    Good luck

    Thanks
    E

  • To find the problem, I would definitely debug the SQL that is being run. You can do this by dumping the value of ‘$loop’

    Thanks
    E

  • I don’t think you have yet explained what type of field is ‘homepage_slide’.

    The type of field is just an Image and I’m including it as a direct path to the image, per example –

    <img src="<?php the_field('homepage_slide'); ?>" alt="slide" />

    Is there a better way to do this?

  • Hi @cutout

    Thanks for the info.

    If the field type is an image, then your meta_query will NOT work. Your meta_query is comparing the value to 1. No image uploaded to WP will ever have a post_id of 1, so this will not work.

    You need to consult the WP_Query args and find the meta_query COMPARE for ‘EXISTS’

    That should work

    Thanks
    E

  • Thanks Elliot. I’ve been getting various answers on this across various forums, but nothing’s working unfortunately. Here are 2 things that I tried, per suggestions –

    $args = array( 'post_type' => 'project', 'meta_query' => array('key' => 'homepage_slide', 'compare' => 'EXISTS'));

    and

    $args = array( 'post_type' => 'project', 'meta_query' => array('key' => 'homepage_slide', 'value' => 'true' , 'compare' => 'LIKE'));

    It’s been awhile since I’ve been stumped by what started as a simple concept! I may not be thinking about this clearly…

  • Hi @cutout

    To find the problem, I would definitely debug the SQL that is being run. You can do this by dumping the value of ‘$loop’

    Thanks
    E

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

The topic ‘Issue with meta_query and ACF’ is closed to new replies.