简体   繁体   English

WordPress查询自定义帖子类型的自定义字段

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

I have a custom post type 'orders' and there are a few custom fields attached to this post type. 我有一个自定义帖子类型“订单”,此帖子类型附带一些自定义字段。

When I query all orders like so: 当我像这样查询所有订单时:

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

I don't get all the data I wish to get. 我没有获得所有想要获得的数据。

I have a tab custom field with a repeater in it. 我在其中有一个中继器的选项卡自定义字段。 And I kinda need the data from the repeater. 我有点需要中继器的数据。 Anyone knows why I don't get this data in the dump of $orders ? 有人知道为什么我没有在$orders转储中得到此数据吗?

Should my query be something else to get all this data? 我的查询应该用来获取所有这些数据吗?

EDIT 编辑

I've managed to query this array: 我设法查询此数组:

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
                )

        )

With this code: 使用此代码:

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

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

What I basically need now is the post_title value from the attractie WP_Post Object and the start and end values. 现在,我基本上需要的是吸引者WP_Post对象中的post_title值以及startend值。

So far so good, you are already creating custom query: 到目前为止,您已经在创建自定义查询:

$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);

Now let us loop through it: 现在让我们循环遍历它:

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..

about ACF Plugin: 关于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 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_field('your_field_name'); //returns value from your custom field get_fields(); //returns array of your fields get_fields(); //returns array of your fields

Note that those ACF plugin functions will work in loop only, in our case when we set up the post with the_post() in while loop everytime. 请注意,在我们的情况下,每次在while循环中使用the_post()设置帖子时,这些ACF插件功能将仅在循环中工作。 Otherwise you need to pass POST_ID as second (in first two) and as first parameters in ACF functions. 否则,您需要传递POST_ID作为第二个(前两个)和ACF函数中的第一个参数。

Also not that we have to reset your query after every custom one. 同样不是我们必须在每个自定义查询后重置您的查询。 And of course to end up our loop. 当然要结束循环。

endwhile; 
wp_reset_query();

References 参考文献

Class Reference/WP Query 类参考/ WP查询

Function Reference/the post 功能参考/职位

ACF Documentation ACF文档

Wordpress The Loop WordPress的循环

EDIT 编辑

To access elements in your array from question you would do this: 要从问题访问数组中的元素,您可以这样做:

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

But this is really NOT the way you should do it. 但这实际上不是您应该采取的方式。

There is a method to get acf fields try using like this 有一种方法可以像这样尝试获取acf字段

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

use get_post_custom function for getting list of custom Fields 使用get_post_custom函数获取自定义字段的列表

get_post_custom($post_id); get_post_custom($ post_id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM