Home › Forums › Add-ons › Repeater Field › How can I query only repeater fields as if they were posts?
So, I’m using repeater field in combination with text box and image gallery. This is so that I can allow my user to create multiple named galleries in a single post.
I would like to make archive of those galleries, and would like to display X galleries on each page.
Problem is that I can’t really query X number of posts that have non empty repeater field, because each such post can carry several galleries.
Is it possible to query just galleries with their names and get them as a flattened result (without being embeded in post object / repeater field)?
No and at the same time yes. It is not possible using most of the standard ACF and WP functions. With an understanding of how repeater fields are stored in the postmeta table you could use $wpdb and build your own SQL queries to get them https://codex.wordpress.org/Class_Reference/wpdb
As far as the names of the fields, lets say that you have a repeater field named “repeater” with a subfield named “subfield”.
In the database there will be multiple entries
meta_key | meta_value
---------------------------------------------------------
repeater | number of rows in the repeater
repeater_0_subfield | content of first row of repeater
repeater_1_subfield | content of second row of repeater
yes… I was afraid of that. Anyway, I made a following sql query which should return “flat” list of all repeater galleries along with their name and post id to which they belong (for generating links and such on the frontend). So, just for the future reference :
select
pm.post_id as post_id
, pm2.meta_value as gallery
, pm.meta_value as images
from
wp_postmeta as pm
inner join
wp_postmeta as pm2
on
((pm.post_id = pm2.post_id) and (pm.meta_key + '_name' = pm2.meta_key))
where
pm.meta_key like 'post_photo_galleries_%_post_photo_gallery'
and
pm2.meta_key like 'post_photo_galleries_%_post_photo_gallery_name';
You’re going to need to check the number of rows in each repeater. The reason is that if a page has 2 galleries and then 1 of them is deleted, you’re query can still return the deleted one. I’m pretty sure that ACF updates the row count, but does not delete the content in any deleted rows. You might want to check just to be sure.
The topic ‘How can I query only repeater fields as if they were posts?’ 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.