"密钥"中的Wordpress WP_Query通配符

rwt*_*rwt 2 wordpress

我有一个简单的问题,但我无法通过网络找到答案.使用WP_Query,"meta_query"中的"key"值如何处理?我可以使用通配符吗?

例如:

$args = array(
    'post-type' => 'post',
    'meta_query' => array(
        array(
            'key' => 'dates_%_participants',
            'compare' => 'LIKE',
            'value' => '"'.$user->ID.'"',
        )
    )
);

$query = new WP_Query($args);
Run Code Online (Sandbox Code Playgroud)

注意'key'中的"%"

Ste*_*eve 8

在要替换的查询中添加过滤器

meta_key ='dates_ $

meta_key喜欢'dates_%

在functions.php中:

function posts_where_dates( $where ) {  
    $where = str_replace("meta_key = 'dates_$", "meta_key LIKE 'dates_%", $where);
    return $where;
}

add_filter( 'posts_where' , 'posts_where_dates' );
Run Code Online (Sandbox Code Playgroud)

您的查询与您的查询保持一致.即

$args = array(
    'post-type' => 'post',
    'meta_query' => array(
        array(
            'key' => 'dates_$_participants',
            'compare' => '=',
            'value' => '"'.$user->ID.'"',  
        )
    )
);
Run Code Online (Sandbox Code Playgroud)

隐藏得很好,但在此处记录:https://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where

由于esc_sql()在WordPress 4.8.3中的更改行为而编辑了答案 https://make.wordpress.org/core/2017/10/31/changed-behaviour-of-esc_sql-in-wordpress-4-8- 3 /