Home › Forums › General Issues › Query Multiple Conditions
I’m trying to filter down a set of posts in a relationship field to “Published Only” + “ACF Field = Something”. So far, I’ve got the “published only” working:
function bm_query_only_published_posts( $args, $field, $post_id ) {
$args['post_status'] = array( 'publish' );
return $args;
}
add_filter( 'acf/fields/post_object/query', 'bm_query_only_published_posts', 10, 3 );
I need to further filter the query to also include only posts where an ACF toggle field “acf_toggle_field_123” is set to “yes”
How/where would I expand the $args to include this?
Where is in your filter that you already have. How is by adding a meta query.
Assuming you are talking about a true false field
$args['meta_query'] = array(
array(
'key' => 'acf_toggle_field_123'
'value' => 1 // true/false save 1/0 in the DB
)
);
Awesome, thanks John! That worked. (Though a comma was needed after the ‘key’ line)
The Toggle was actually a radio button with “Yes” and “No”, but I get it with true/false. The final query arg ended up being:
function bem_query_only_published_posts( $args, $field, $post_id ) {
$args['post_status'] = array( 'publish' );
$args['meta_query'] = array(
array(
'key' => 'acf_toggle_field_123',
'value' => 'Yes'
)
);
return $args;
}
add_filter( 'acf/fields/post_object/query', 'bem_query_only_published_posts', 10, 3 );
Thanks again John. My initial question was answered, but if you’re still willing…
The results are sorted alpha by Title (default) I’d like to sort the results by yet another ACF which is a date/time field = ‘start_date_time’
(These are a CPT called “Events” and each event has a start / end date & time. I’d like to sort the results in order of the date/time that each Event takes place)
I don’t know where to put ‘orderby’ values, etc. Not a biggie, but gosh I wish I knew more PHP than novice level 🙂
You must be logged in to reply to this topic.
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.