【问题标题】:wordpress handle ACF custom fields of type relationship with WP_Querywordpress 处理与 WP_Query 类型关系的 ACF 自定义字段
【发布时间】:2019-01-21 07:00:23
【问题描述】:

我在我的一个函数中使用了 WP_Query。

function get_id_list ($postType) {
    $wpb_all_query = new WP_Query(
        array(
            'post_type' => $postType,
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'meta_query' => array(
                array(
                    //'key' => 'title',
                    //'value' => 'Wordpress Development BSP Project', // this works and the function only returns a list with the single ID of the post where this matches
                    'key' => 'bicslab_axis',
                    'value' => '22', // this does not work
                    'value' => 'greenware', // this does not work

                )
            )
        )
    );
    $postIDList = [];
    if($wpb_all_query->have_posts()){
        while ( $wpb_all_query->have_posts() ) {
            $wpb_all_query->the_post();
            $postIDList[] = get_the_ID();
        }
        wp_reset_postdata();
    }
    print_r($postIDList);
    return $postIDList;
}

当我在'meta_query' 中选择一个“文本”类型的字段时,查询可以正常工作,但如果我选择“关系”类型的字段,则不会。

这是我的高级自定义字段的屏幕截图:

对于我尝试输入 ID 和名称的关系字段,它们都不起作用。

如何使查询仅检索类型为关系的自定义字段仅与某些对象相关的帖子?

编辑:我应该澄清一下,我在我的 mea 查询中分别使用了 "22""greenware" 作为值,因为 22 是 ID,greenware 是具有该 ID 的特定博客文章/对象的“名称”

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    这是因为您在 object 中搜索 string

    使用 WP_Query 参数

    可以只加载选定的帖子 ID,而不是帖子对象。这样,您可以在 WP_Query 中使用 ID 并指定参数,例如 posts_per_page、order 和 orderby。要了解有关 WP_Query 参数的更多信息,请阅读http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

    请注意,get_field 函数有 2 个错误参数。第一个参数是 $post_id 并且不相关,但第二个参数是告诉 ACF 不要格式化该值,并且只返回 DB 中的内容(ID 数组)

    <?php 
    
    // get only first 3 results
    $ids = get_field('conference_talks', false, false);
    
    $query = new WP_Query(array(
        'post_type'         => 'conferences',
        'posts_per_page'    => 3,
        'post__in'          => $ids,
        'post_status'       => 'any',
        'orderby'           => 'post__in',
    ));
    
    ?>
    

    来源:https://www.advancedcustomfields.com/resources/relationship/

    【讨论】:

    • 我一直在使用您的代码并对其进行试验 - 没有成功。我们正在谈论同样的事情,您会在我的查询中添加什么以使其正常工作?我已将关系设置为返回帖子 ID 而不是帖子对象。 (imgur.com/d2BzC6F) 因此,如果您的 $id 工作方式相似,它将返回一个包含 id 的字符串。在那种情况下,为什么我的硬编码字符串“22”不起作用?
    • 是的,我看了一下,看到你设法弄明白了,我玩了'IN',因为关系字段会返回数组但仍然没有运气。
    • 顺便说一句,既然您看到了我可能的解决方案,您可能知道它为什么有效吗? wordpress.stackexchange.com/questions/70864/… 这是LIKE 运算符的解释,但我不明白value 是什么。根据我的理解,我可以推断value 以 22 开头,但我不知道如何找到有关它的更多信息。
    • 从查询中排除元信息,然后在其中一个字段上使用var_dump(get_field('bicslab_axis', $ID))
    • 好吧,我只是没有进行一些研究 - 看起来您的解决方案是正确的查询方式。 (见底部:advancedcustomfields.com/resources/querying-relationship-fields
    【解决方案2】:

    我找到了解决办法:WordPress query posts by ACF

    我唯一需要更改的是 'value''compare' 参数,来自:

    'value' =>'22',
    'compare' => '=',
    

    到这里:

    'value' =>'"'.'22'.'"',
    'compare' => 'LIKE',
    

    编辑:它甚至可以在没有强制引号的情况下工作:

    'value' => '22',
    'compare' => 'LIKE',
    

    这是完整的代码:

    function get_id_list ($postType) {
        $wpb_all_query = new WP_Query(
            array(
                'post_type' => $postType,
                'post_status' => 'publish',
                'posts_per_page' => -1,
                'meta_query' => array(
                    array(
                        'key' => 'bicslab_axis',
                        'value' => '22',
                        'compare' => 'LIKE',
    
                    )
                )
            )
        );
        $postIDList = [];
        if($wpb_all_query->have_posts()){
            while ( $wpb_all_query->have_posts() ) {
                $wpb_all_query->the_post();
                $postIDList[] = get_the_ID();
            }
            wp_reset_postdata();
        }
        print_r($postIDList);
        return $postIDList;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-01
      • 2020-10-18
      • 2016-06-08
      • 1970-01-01
      • 2020-04-20
      • 2017-06-20
      • 1970-01-01
      • 2018-10-02
      • 2013-09-29
      相关资源
      最近更新 更多