Home › Forums › General Issues › get select value in meta query
hi everyone,
i want to get my post with custom query, but when i filter posts with the meta value that has an array, my posts doesn’t show anymore.
$args = array(
'post_type' => 'cafe',
'posts_per_page' => 12,
'rewrite' => array( 'slug' => 'cafe', 'with_front' => true),
'meta_query' => array(
array(
'key' => 'cafe_state',
'value' => $cafe_state,
'compare' => 'LIKE',
),
array(
'key' => 'cafe_area',
'value' => $cafe_area,
'compare' => 'LIKE',
),
array(
'key' => 'cafe_name',
'value' => $cafe_name,
'compare' => 'LIKE',
),
array(
'key' => 'cafe_facilities',
'value' => array( 'test' ),
'compare' => 'IN',
),
),
);
$query = new wp_query( $args );
I’m guessing that you mean the cafe_facilities
field? Is this a checkbox field or some other type of field that has multiple values?
Querying a checkbox field is similar to querying a relationship field because it stores a serialized array https://www.advancedcustomfields.com/resources/querying-relationship-fields/
$args = array(
'post_type' => 'cafe',
'posts_per_page' => 12,
'rewrite' => array( 'slug' => 'cafe', 'with_front' => true),
'meta_query' => array(
array(
'key' => 'cafe_state',
'value' => $cafe_state,
'compare' => 'LIKE',
),
array(
'key' => 'cafe_area',
'value' => $cafe_area,
'compare' => 'LIKE',
),
array(
'key' => 'cafe_name',
'value' => $cafe_name,
'compare' => 'LIKE',
),
array(
'key' => 'cafe_facilities',
'value' => '"test"',
'compare' => 'LIKE',
),
),
);
That depends on if you want to get posts with both values or with either value (AND/OR).
// AND
$args = array(
'post_type' => 'cafe',
'posts_per_page' => 12,
'rewrite' => array( 'slug' => 'cafe', 'with_front' => true),
'meta_query' => array(
array(
'key' => 'cafe_state',
'value' => $cafe_state,
'compare' => 'LIKE',
),
array(
'key' => 'cafe_area',
'value' => $cafe_area,
'compare' => 'LIKE',
),
array(
'key' => 'cafe_name',
'value' => $cafe_name,
'compare' => 'LIKE',
),
array(
'key' => 'cafe_facilities',
'value' => '"test"',
'compare' => 'LIKE',
),
array(
'key' => 'cafe_facilities',
'value' => '"tests"',
'compare' => 'LIKE',
),
),
);
// OR
$args = array(
'post_type' => 'cafe',
'posts_per_page' => 12,
'rewrite' => array( 'slug' => 'cafe', 'with_front' => true),
'meta_query' => array(
array(
'key' => 'cafe_state',
'value' => $cafe_state,
'compare' => 'LIKE',
),
array(
'key' => 'cafe_area',
'value' => $cafe_area,
'compare' => 'LIKE',
),
array(
'key' => 'cafe_name',
'value' => $cafe_name,
'compare' => 'LIKE',
),
array(
'relation' => 'OR',
array(
'key' => 'cafe_facilities',
'value' => '"test"',
'compare' => 'LIKE',
),
array(
'key' => 'cafe_facilities',
'value' => '"tests"',
'compare' => 'LIKE',
),
),
),
);
You should be cautious when adding additional meta queries because the slow down site performance.
The topic ‘get select value in meta query’ is closed to new replies.
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.