按子字段值 ACF 的元查询帖子
Meta Query Posts by Sub Field Value ACF
我正在尝试查询子字段值为 'audi' 的帖子。我看了又看,但找不到答案。我拥有的 $args 在下面,并且数据库中存在与 'audi' 匹配的帖子作为转发器 'cars'.
的子字段 'model' 的值
$args = array(
'post_type' => 'manufacturers',
'meta_query' => array(
array(
'key' => 'cars_%_model',
'value' => 'audi',
'compare' => 'LIKE'
)
)
);
$query = new WP_Query($args);
任何有关此代码在哪里出错的提示都将不胜感激。
我为此花了几个小时,并为您提供了解决方案:
您需要做的第一件事是设置一个过滤器,将“=”比较替换为 'LIKE' 比较(将其包含在您之前的代码之上):
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'cars_", "meta_key LIKE 'cars_", $where);
// note if using wordpress < v4.8.3 add a % to the meta key like this: meta_key = 'cars_%",...
return $where;
}
add_filter('posts_where', 'my_posts_where');
现在您需要做的就是将 meta_query 比较更新为“=”:
$args = array(
'post_type' => 'manufacturers',
'meta_query' => array(
array(
'key' => 'cars_%_model',
'value' => 'audi',
'compare' => '='
)
)
);
$query = new WP_Query($args);
这包含在 ACF documentation however the documentation does not mention the updates following Wordpress 4.8.3 which is mentioned here 中。
我正在尝试查询子字段值为 'audi' 的帖子。我看了又看,但找不到答案。我拥有的 $args 在下面,并且数据库中存在与 'audi' 匹配的帖子作为转发器 'cars'.
的子字段 'model' 的值$args = array(
'post_type' => 'manufacturers',
'meta_query' => array(
array(
'key' => 'cars_%_model',
'value' => 'audi',
'compare' => 'LIKE'
)
)
);
$query = new WP_Query($args);
任何有关此代码在哪里出错的提示都将不胜感激。
我为此花了几个小时,并为您提供了解决方案:
您需要做的第一件事是设置一个过滤器,将“=”比较替换为 'LIKE' 比较(将其包含在您之前的代码之上):
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'cars_", "meta_key LIKE 'cars_", $where);
// note if using wordpress < v4.8.3 add a % to the meta key like this: meta_key = 'cars_%",...
return $where;
}
add_filter('posts_where', 'my_posts_where');
现在您需要做的就是将 meta_query 比较更新为“=”:
$args = array(
'post_type' => 'manufacturers',
'meta_query' => array(
array(
'key' => 'cars_%_model',
'value' => 'audi',
'compare' => '='
)
)
);
$query = new WP_Query($args);
这包含在 ACF documentation however the documentation does not mention the updates following Wordpress 4.8.3 which is mentioned here 中。