【问题标题】:ACF Field query by Repeater Field not empty转发器字段查询的 ACF 字段不为空
【发布时间】:2016-06-17 04:04:07
【问题描述】:

一直在阅读有关在 wordpress here 中查询 ACF 字段的信息,但遇到此问题:

$args = array(
    'posts_per_page' => -1,
    'post_type' => 'team_member',
    'status' => 'publish',
    'orderby' => 'title',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'patents',
            'value' => array(''),
            'compare' => 'NOT IN'
        )
    )
);

$the_query = new WP_Query($args);

基本上,只是尝试查询所有具有 ACF 中继器字段的帖子,称为 patents,其中至少有 1 项专利。如何做到这一点?

【问题讨论】:

    标签: advanced-custom-fields wordpress


    【解决方案1】:

    我只需要自己处理这个问题,最终对我有用的是查询所有转发器字段值大于 0 的帖子。

    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'team_member',
        'status' => 'publish',
        'orderby' => 'title',
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'patents',
                'value' => 0
                'compare' => '>'
            )
        )
    );
    

    Mark's answer 搜索 'patents' = 1 只返回转发器字段具有一个值的帖子。

    【讨论】:

    • 谢谢,这看起来很有希望。将不得不记住这一点。 :)
    【解决方案2】:

    http://www.advancedcustomfields.com/resources/the_repeater_field/

    尝试先访问中继器字段,然后调用 WP_Query。如果您要执行以下操作:

    if( get_field('repeater_field_name', $post_id) )
    

    然后调用 sub_field 和/或在本例中为 WP_Query。

    还有这种语法,你可以收集所有你需要的帖子然后滚动浏览帖子:

    if( have_rows('repeater_field') ):
    
    while( have_rows('repeater_field') ) : the_row();
    
        $sub = get_sub_field('sub_field');
    

    也许你可以在这个级别调用 wp_query?

    【讨论】:

    • 那不行,因为我需要访问所有没有空转发器字段的帖子类型,如果不先查询帖子,我无法获得帖子的$post_id
    • 谢谢,我现在会研究tax_query 选项。
    【解决方案3】:

    只需检查值是否为true

    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'team_member',
        'status' => 'publish',
        'orderby' => 'title',
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'patents',
                'value' => true,
                'compare' => '='
            )
        )
    );
    
    $the_query = new WP_Query($args);
    

    【讨论】:

    • 这给了我一个空的结果,当我这样做时,我肯定在patents转发器字段中有一些带有转发器项目的帖子。
    • 此代码将为您提供至少有 1 行 acf 转发器归档的任何帖子(专利)
    【解决方案4】:

    试试这个..

    这不是实现目标的标准解决方案,但它会 工作

    获取所有转发器字段存在的帖子 ID

    <?php 
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'team_member',
        'status' => 'publish',
        'orderby' => 'title',
        'order' => 'ASC',
    );
    
    $the_query = new WP_Query($args);
    
    // The Loop
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            if ( have_rows('patents') ){
                $allowed_ids[] = get_the_ID();
            }
        }
    } 
    /* Restore original Post Data */
    wp_reset_postdata();?>
    

    然后运行另一个循环以仅通过其 IDS 获取这些帖子

     $args['post__in'] = $allowed_ids;
    
        $the_query = new WP_Query($args);
    // The Loop
        if ( $the_query->have_posts() ) {
            while ( $the_query->have_posts() ) {
                $the_query->the_post();
    $patents =  get_field('patents');//array
    
    }}
    

    【讨论】:

    • 谢谢,但这并不能帮助我获得至少包含 1 个项目的 ACF 中继器字段。这只是获取帖子的 ID。
    • 在第二个循环中,您可以获得中继器值。查看编辑后的答案
    猜你喜欢
    • 2017-02-22
    • 2018-10-06
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 2019-05-24
    • 2018-07-17
    相关资源
    最近更新 更多