Support

Account

Forum Replies Created

  • Adding another vote for please making the button text customizable without a jQuery hack. Thanks!

  • 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');
  • For anyone else who comes across this solution when trying to search for repeater sub-fields:

    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.

  • Hi @jonathan,

    I know this is an old thread, but your explanation and solution were nice and clear, so I’m hoping you’re still around for follow-up…

    What if the sub-field of the repeater is a relationship field?
    In that case what’s the proper way to set the meta_value for matching?

    Going on the example above, let’s say the value is not simply a string like “GRAND PRIZE” but rather an instance of a custom post type called “Prize Levels”.

    Thanks,
    Alison

Viewing 4 posts - 1 through 4 (of 4 total)