[英]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
值以及start
和end
值。
到目前为止,您已经在创建自定义查询:
$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();
参考文献
编辑
要从问题访问数组中的元素,您可以这样做:
$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.