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();
}
?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.