You can try and filter the (meta) query and do a replace on the where clause:
http://codex.wordpress.org/Function_Reference/get_meta_sql
Try it with this in your functions.php:
function filter_meta_query( $pieces ) {
if ( !empty( $pieces['where'] ) ) {
$pieces['where'] = preg_replace( '/(.*?)LIKE \'\%(.*?)\%\'/', "\$1LIKE '\$2%'", $pieces['where'] );
}
return $pieces;
}
Use the filter just before you use the meta query and remove it after the query:
<?php
add_filter( 'get_meta_sql', 'filter_meta_query' );
$args = array(
'meta_query' => array(
array(
'key' => 'surname',
'compare' => 'LIKE',
'value' => 'ab'
)
)
);
// the query
$query = new WP_Query( $args );
// remove it after the query.
remove_filter( 'get_meta_sql', 'filter_meta_query' );
// the loop
while ( $query->have_posts() ) : $query->the_post(); ?>
Thread Starter
Joms
(@jomsky)
This worked well! The filterget_meta_sql will surely be helpful to my queries from now on. Thanks!
You’re welcome. I’m glad you’ve got it resolved.