Support

Account

Home Forums General Issues get_pages filtered by ACF field Reply To: get_pages filtered by ACF field

  • 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();
    }
    ?>