按子字段值ACF的元查询帖子 [英] Meta Query Posts by Sub Field Value ACF

查看:80
本文介绍了按子字段值ACF的元查询帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按子字段值为 audi的人查询帖子。我看了又看,但是找不到答案。我的$ args在下面,数据库中存在与 audi匹配的帖子,作为中继器 cars的子字段 model的值。

I am attempting to query posts by those with sub field values of 'audi'. I have looked and looked but cannot find an answer. The $args I have are below, and posts exist in the database which match 'audi' as the value for sub field 'model' of repeater 'cars'.

$args = array(
    'post_type' => 'manufacturers',
    'meta_query' => array(
       array(
        'key' => 'cars_%_model',
        'value' => 'audi',
        'compare' => 'LIKE'
       )
    )
);
$query = new WP_Query($args);

任何有关此代码降级的提示都将不胜感激。

Any hints as to where this code falls down would be really appreciated.

推荐答案

我花了几个小时为您提供解决方案:

I spent a few hours on this and have a solution for you:

第一件事需要做的是设置一个过滤器,将 =比较替换为 LIKE比较(在您之前的代码上方包含此内容):

First thing you need to do is setup a filter which replaces '=' comparisons with 'LIKE' comparisons (include this above your previous code):

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比较更新为'=':

Now all you need to do is update your meta_query comparison to '=':

$args = array(
    'post_type' => 'manufacturers',
    'meta_query' => array(
       array(
        'key' => 'cars_%_model',
        'value' => 'audi',
        'compare' => '='
       )
    )
);
$query = new WP_Query($args);

这包含在 ACF文档,但是该文档没有提及Wordpress 4.8.3之后的更新此处提到的

This is included in the ACF documentation however the documentation does not mention the updates following Wordpress 4.8.3 which is mentioned here.

这篇关于按子字段值ACF的元查询帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆