Support

Account

Home Forums General Issues get_pages filtered by ACF field

Solving

get_pages filtered by ACF field

  • Dear all,

    I’m trying to do something which seems very simple but I must be too new to succeed on my own and I haven’t found any answer in this forum.

    What I’m trying to do is entirely explained in the title. I’d like to gat all the pages that have a certain value for an ACF checkbox field I added to all content of type “page”.

    Is there any way to do that ?

    Thanks a lot for your help !

  • This reply has been marked as private.
  • Hi @benoitmorel

    You can (and should) achieve this by using wp_query with a meta_query parameter. get_posts is a wrapper for wp_query so there’s not really any point using it for this purpose.

    Here’s a stripped down code example you can use to get started. Inside the while-loop you’re in “the loop” in WordPress and can use any loop-specific functions like the_title(), get_permalink() and get_the_content() (for example).

    
    <?php
    $args = array(
        'post_type' => 'page',
        'post_status' => 'publish',
        'meta_query' => array(
        	array(
    	        'key' => 'YOURKEY',
    	        'value' => 'YOURVALUE',
    	        'compare' => 'LIKE'
    	    )
    	)
    );
    $similar_query = new WP_Query($args);
    
    if( $similar_query->have_posts() ){
    	
    	while( $similar_query->have_posts() ){
    		$similar_query->the_post();
    		
    		the_title();
    		
    	}
    	wp_reset_postdata();
    }
    ?>
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.