簡體   English   中英

WordPress查詢自定義帖子類型的自定義字段

[英]WordPress query custom fields of a custom post type

我有一個自定義帖子類型“訂單”,此帖子類型附帶一些自定義字段。

當我像這樣查詢所有訂單時:

$orders = new WP_Query( array( 'post_type' => 'orders') );

我沒有獲得所有想要獲得的數據。

我在其中有一個中繼器的選項卡自定義字段。 我有點需要中繼器的數據。 有人知道為什么我沒有在$orders轉儲中得到此數據嗎?

我的查詢應該用來獲取所有這些數據嗎?

編輯

我設法查詢此數組:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [attractie] => WP_Post Object
                        (
                            [ID] => 41
                            [post_author] => 1
                            [post_date] => 2016-02-29 14:30:33
                            [post_date_gmt] => 2016-02-29 14:30:33
                            [post_content] => <h2>Title!</h2>
                            Content.
                            [post_title] => Post Title
                            [post_excerpt] => 
                            [post_status] => publish
                            [comment_status] => closed
                            [ping_status] => closed
                            [post_password] => 
                            [post_name] => post-name
                            [to_ping] => 
                            [pinged] => 
                            [post_modified] => 2016-04-07 14:39:41
                            [post_modified_gmt] => 2016-04-07 14:39:41
                            [post_content_filtered] => 
                            [post_parent] => 0
                            [guid] => url
                            [menu_order] => 0
                            [post_type] => attracties
                            [post_mime_type] => 
                            [comment_count] => 0
                            [filter] => raw
                        )

                    [start] => 0930
                    [end] => 1200
                )

            [1] => Array
                (
                    [attractie] => 
                    [start] => 0930
                    [end] => 1200
                )

        )

使用此代碼:

$orders = new WP_Query( array( 'post_type' => 'orders') );

foreach ( $orders->posts as $all ) {
   $fields[] = get_field( 'planning', $all->ID );
}

現在,我基本上需要的是吸引者WP_Post對象中的post_title值以及startend值。

到目前為止,您已經在創建自定義查詢:

$loopargs = array( //in the array are arguments for custom query            
   'post_type' => 'orders', //your CPT
   'posts_per_page' => -1, //lets make WP show them all
   'orderby' => 'date', // newest
   'order' => 'DESC', //to the top
); 
$query = new WP_Query($loopargs);

現在讓我們循環遍歷它:

while ($query->have_posts()) : $query->the_post(); //the_post loads for you current post in loop so that you can use functions like the_title(), the_content(), etc..

關於ACF插件:

the_field('your_field_name'); //will output field content directly the_field('your_field_name'); //will output field content directly get_field('your_field_name'); //returns value from your custom field get_field('your_field_name'); //returns value from your custom field get_fields(); //returns array of your fields get_fields(); //returns array of your fields

請注意,在我們的情況下,每次在while循環中使用the_post()設置帖子時,這些ACF插件功能將僅在循環中工作。 否則,您需要傳遞POST_ID作為第二個(前兩個)和ACF函數中的第一個參數。

同樣不是我們必須在每個自定義查詢后重置您的查詢。 當然要結束循環。

endwhile; 
wp_reset_query();

參考文獻

類參考/ WP查詢

功能參考/職位

ACF文檔

WordPress的循環

編輯

要從問題訪問數組中的元素,您可以這樣做:

$title = $array[0][0]['attractie']->post_title;
$start = $array[0][0]['start'];
$end = $array[0][0]['end'];

但這實際上不是您應該采取的方式。

有一種方法可以像這樣嘗試獲取acf字段

$orders = new WP_Query( array( 'post_type' => 'orders') );
foreach ( $orders->posts as $allPosts ) {
$field     = get_field( 'your field name', $allPosts->ID );
}

使用get_post_custom函數獲取自定義字段的列表

get_post_custom($ post_id);

暫無
暫無

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

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