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.
    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(
            '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 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');