Home › Forums › Add-ons › Repeater Field › Query posts based on two subfields displays extra post without these values
Hello, I’ve got the following problem while querying with repeater subfields:
My aim is to query posts where the subfield ‘age’ (leeftijd) (which is a radio button) is ‘4-6’, ‘7-9′, ’10-12’, or ‘4-12’.
Then it should also query whether it has a subfield ‘subject’ (onderwerp) (which are checkboxes), like ‘inrichten’, ‘communicatie’, and ‘regels’.
So for example I have a post where the age is ‘7-9’, and the subjects in that row are ‘inrichten’ and ‘communicatie’. It also has ‘age’: ‘4-6’ and ‘subject’: ‘regels’ (which is why I use a repeater field for this).
It should show the post whether I query on ‘inrichten’ or ‘communicatie’.
I’ll use the following query (with ‘inrichten’):
<?php function my_posts_where( $where ) {
$where = str_replace("meta_key = 'gegevens_%", "meta_key LIKE 'gegevens_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where'); ?>
<?php $args = array(
'post_type' => 'spelvormen',
'post_status' => 'public',
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' => 'title',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'gegevens_%_leeftijd', //radio
'compare' => '=',
'value' => '7-9',
),
array(
'key' => 'gegevens_%_onderwerp', //checkbox
'compare' => 'LIKE',
'value' => 'inrichten',
),
),
);
$query = new WP_Query( $args ); ?>
This works, it shows a post which has both values. The problem, however, I have two more posts (a total of 3), where the third post doesn’t have the subject ‘inrichten’ (where age is ‘7-9’).
But it is still being shown in the frontend, while it shouldn’t. Did I do something wrong in my query? I’m using this for a ajax form where people can select the age and the subject, and then it should display posts which have these values.
The form works, but as I said before, it displays an extra post which should’nt be displayed.
Hi @dejong12
Could you please share the JSON export file of your field group so I can test it out on my installation? Also, could you please share the screenshots of the backend so I know the data I should set for each post?
Thanks 🙂
Sure, here is the JSON file: http://pastebin.com/2FFJ5up3.
And the data for the posts: http://imgur.com/a/oI9k4
Do you need some extra input or are the files I posted here sufficient?
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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.