Home › Forums › Add-ons › Repeater Field › If Subfields Equals, Show Posts › Reply To: If Subfields Equals, Show Posts
Following up on my previous post, I’ve found the solution through a number of different sources. Noting it here in case anyone else has the same question:
I was daunted by the fact that relationship info was stored as a serialized array e.g.
a:2:{i:0;s:2:”35″;i:1;s:2:”33″;}
but this post makes it clear that it’s as simple as putting the ID of the sought-after post in quotation marks (for exact match) and doing a LIKE comparison, e.g.:
'meta_query' => array(
array(
'key' => "repeaterfieldname_XYZ_subfieldname",
'compare' => 'LIKE',
'value' => '"' . $post_id . '"', // matches exactly "123". This prevents a match for "1234"
)
)
(in other words, since the serialized array is treated just like any other string in running the DB query, you just search for the post ID as a fragment of that string.)
Note that due to changes in WordPress, you can no longer use the percent sign as your wildcard. The percent sign will be substituted with a hash and your query will come up empty. So, see http://www.advancedcustomfields.com/resources/query-posts-custom-fields/ for a related example, which suggests using the dollar sign instead.
Since the dollar sign is used to indicate variable names in php, though, and can therefore cause confusion if you’re using a text editor with syntax highlighting, I prefer to use a string like XYZ for the substitution instead.
So my filter is as follows:
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'repeaterfieldname_XYZ", "meta_key LIKE 'repeaterfieldname_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
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.