Home › Forums › ACF PRO › Querying the database for repeater sub field values – shows duplicate results
I followed the Querying the database for repeater sub field values tutorial and it seemed to work like a charm. The only problem is that the query is returning multiple duplicate results for each single result.
Example:
I have a repeater field called “Friend / Partner Entry”.
Inside the repeater I have TITLE, LOGO and TEXT.
When I do the query from the tutorial, I get something like this:
first loop:
TITLE 1
TITLE 1
TITLE 1
TITLE 1
TITLE 1
second loop:
TITLE 2
TITLE 2
TITLE 2
third loop:
TITLE 3
TITLE 3
and it continues for all results after.
I did a var dump and it shows the objects multiple times. The meta_id and post_id are different for each duplicate result, but the meta_key is correct. I don’t entirely follow this but I can’t seem to remove the duplicates (even temporarily). Can’t do an array_unique on objects and I tried a trick I found on Stack that says to json encode the array and THEN array unique and then decode it back, but that dosn’t work either (most likely because the meta_id’s are all different right?).
Any help would be greatly appreciated!
(partial array dump below)
Array
(
[0] => stdClass Object
(
[meta_id] => 8386
[post_id] => 5894
[meta_key] => friend_partner_entry_0_show_logo_on_home
[meta_value] => 1
)
[1] => stdClass Object
(
[meta_id] => 8404
[post_id] => 30
[meta_key] => friend_partner_entry_0_show_logo_on_home
[meta_value] => 1
)
[2] => stdClass Object
(
[meta_id] => 8422
[post_id] => 5895
[meta_key] => friend_partner_entry_0_show_logo_on_home
[meta_value] => 1
)
[3] => stdClass Object
(
[meta_id] => 8442
[post_id] => 5897
[meta_key] => friend_partner_entry_0_show_logo_on_home
[meta_value] => 1
)
[4] => stdClass Object
(
[meta_id] => 8568
[post_id] => 5924
[meta_key] => friend_partner_entry_0_show_logo_on_home
[meta_value] => 1
)
[5] => stdClass Object
(
[meta_id] => 8456
[post_id] => 5897
[meta_key] => friend_partner_entry_1_show_logo_on_home
[meta_value] => 1
)
[6] => stdClass Object
(
[meta_id] => 8488
[post_id] => 30
[meta_key] => friend_partner_entry_1_show_logo_on_home
[meta_value] => 1
)
[7] => stdClass Object
(
[meta_id] => 8582
[post_id] => 5924
[meta_key] => friend_partner_entry_1_show_logo_on_home
[meta_value] => 1
)
[8] => stdClass Object
(
[meta_id] => 8708
[post_id] => 5924
[meta_key] => friend_partner_entry_10_show_logo_on_home
[meta_value] => 1
)
[9] => stdClass Object
(
[meta_id] => 9076
[post_id] => 30
[meta_key] => friend_partner_entry_10_show_logo_on_home
[meta_value] => 1
)
I forgot to post my query:
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM {$wpdb->prefix}postmeta
WHERE meta_key LIKE %s
AND meta_value = %s
",
'friend_partner_entry_%_show_logo_on_home', // meta_name: $ParentName_$RowNumber_$ChildName
'1' // meta_value: 'type_3' for example
));
I might have fixed it myself, hopefully someone can tell me if this is wise or not, but I added to the query from the tutorial:
GROUP BY meta_key
the the full query is now:
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM {$wpdb->prefix}postmeta
WHERE meta_key LIKE %s
AND meta_value = %s
GROUP BY meta_key",
'friend_partner_entry_%_show_logo_on_home', // meta_name: $ParentName_$RowNumber_$ChildName
'1' // meta_value: 'type_3' for example
));
Details about the cause of this (similar) “issue:” https://support.advancedcustomfields.com/forums/topic/acf-repeater-database-query/#post-45116
The topic ‘Querying the database for repeater sub field values – shows duplicate results’ 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.