代码之家  ›  专栏  ›  技术社区  ›  charelf

wordpress处理与WP_Query类型相关的ACF自定义字段

  •  0
  • charelf  · 技术社区  · 6 年前

    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'

    以下是我的高级自定义字段的屏幕截图: enter image description here

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

    如何使查询只检索类型为relation的自定义字段只与某些对象相关的posts?

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

    2 回复  |  直到 6 年前
        1
  •  1
  •   Gavin Thomas    6 年前

    因为你在寻找 string 在一个 object

    使用WP_查询参数

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

    <?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/

        2
  •  0
  •   charelf    6 年前

    我找到了解决办法: 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;
    }