Last active
December 25, 2015 02:59
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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