簡體   English   中英

wordpress處理與WP_Query類型相關的ACF自定義字段

[英]wordpress handle ACF custom fields of type relationship with WP_Query

我在其中一個函數中使用了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和名稱,但它們都不起作用。

如何使查詢僅檢索類型關聯的自定義字段僅與某些對象相關的帖子?

編輯:我應該澄清一下,我在"22" mea”查詢中分別使用"22""greenware"作為值,因為22是ID,而greenware是具有該ID的特定博客文章/對象的“名稱”

這是因為您要在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不要格式化該值,並且僅返回數據庫中的內容(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/

我找到了解決方案: ACF的WordPress查詢帖子

我唯一需要更改的是'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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM