Skip to content

Instantly share code, notes, and snippets.

@codearachnid
Last active December 25, 2015 02:59
Show Gist options
  • Save codearachnid/6906929 to your computer and use it in GitHub Desktop.
Save codearachnid/6906929 to your computer and use it in GitHub Desktop.
Fix Advanced Custom Field dynamic key filter for repeater entries automagically. See example 5 http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/
<?php
/*
* Fix Advanced Custom Field dynamic key filter for repeater entries
* @link http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/
*/
add_filter( 'posts_where', 'acf_repeater_dynamic_where', 10, 2 );
function acf_repeater_dynamic_where( $where, &$wp_query ){
if( !empty( $wp_query->meta_query->queries ) ){
$keys = wp_list_pluck( $wp_query->meta_query->queries, "key" );
foreach( $keys as $key ){
if( strpos( $key, '%') !== false ){
// custom filter to replace '=' with 'LIKE' for key
$where = str_replace("meta_key = '{$key}'", "meta_key LIKE '{$key}'", $where);
}
}
}
return $where;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment