问题描述
我知道我可以在 WP_Query 中使用 REGEXP,像这样:
$query = new WP_Query(array(
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'custom_fields',
'value' => 'foo[(][0-9][)]', // with regex stuff
'compare' => 'REGEXP',
),
),
));
但是我也需要正则表达式。喜欢这个:
$query = new WP_Query(array(
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'custom_fields[(][0-9][)]', // with regex stuff
'value' => 'foo',
'compare' => 'REGEXP',
),
),
));
有什么办法可以实现这个过滤器吗?或者我必须自己用自己的 SQL 查询来构建它?
最佳解决方案
这是一个实验的想法:
假设我们得到:
post A with the custom field
location1
as UK – Londonpost B with the custom field
location2
as France – Parispost C with the custom field
location3
as USA – New York
那么我们可以使用,例如:
$args = [
'meta_query' => [
'relation' => 'OR',
[
'key' => "^location[0-9]",
'_key_compare' => 'REGEXP',
'value' => 'London',
'compare' => 'LIKE',
],
[
'key' => 'location%',
'_key_compare' => 'LIKE',
'value' => 'Paris',
'compare' => 'LIKE'
],
[
'key' => 'location3',
'value' => 'New York',
'compare' => 'LIKE'
]
]
];
我们用以下插件支持自定义_key_compare
参数:
<?php
/**
* Plugin Name: Extended Meta Key Search In WP_Query
* Description: Custom '_key_compare' argument as REGEXP, RLIKE or LIKE
* Plugin URI: http://wordpress.stackexchange.com/a/193841/26350
* Plugin Author: Birgir Erlendsson (birgire)
* Version: 0.0.3
*/
add_action( 'pre_get_posts', function( $q )
{
// Check the meta query:
$mq = $q->get( 'meta_query' );
if( empty( $mq ) )
return;
// Init:
$marker = '___tmp_marker___';
$rx = [];
// Collect all the sub meta queries, that use REGEXP, RLIKE or LIKE:
foreach( $mq as $k => $m )
{
if( isset( $m['_key_compare'] )
&& in_array( strtoupper( $m['_key_compare'] ), [ 'REGEXP', 'RLIKE', 'LIKE' ] )
&& isset( $m['key'] )
) {
// Mark the key with a unique string to secure the later replacements:
$m['key'] .= $marker . $k; // Make the appended tmp marker unique
// Modify the corresponding original query variable:
$q->query_vars['meta_query'][$k]['key'] = $m['key'];
// Collect it:
$rx[$k] = $m;
}
}
// Nothing to do:
if( empty( $rx ) )
return;
// Get access the generated SQL of the meta query:
add_filter( 'get_meta_sql', function( $sql ) use ( $rx, $marker )
{
// Only run once:
static $nr = 0;
if( 0 != $nr++ )
return $sql;
// Modify WHERE part where we replace the temporary markers:
foreach( $rx as $k => $r )
{
$sql['where'] = str_replace(
sprintf(
".meta_key = '%s' ",
$r['key']
),
sprintf(
".meta_key %s '%s' ",
$r['_key_compare'],
str_replace(
$marker . $k,
'',
$r['key']
)
),
$sql['where']
);
}
return $sql;
});
});
我们在字符串替换的每个元键上添加唯一的标记。
请注意,这不支持正则表达式字符转义,如 (
和\
。
参考文献
注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。