Forked from codearachnid/acf_repeater_dynamic_where.php
Created
November 2, 2015 17:57
-
-
Save tazeverywhere/7ed3006dca898c715e84 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 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