douwei9759
2018-10-23 21:00
浏览 198
已采纳

我试图找到如何从我的每个自定义帖子类型中提取对象(WP_Post)#,以便我可以使用ACF get_fields()函数

I am trying to to find how to extract the object(WP_Post)# from each $post of my $posts array in the function below so that I can assign it to a variable and add it to the parameter of the get_field() function. This is not a complete function, I have just been using var_dump to test.

<?php

function zip_search($userZip){
    $posts = get_posts(array(
     'posts_per_page'    => -1,
     'post_type'         => 'Location'
    ));


    if( $posts ) {

        foreach( $posts as $post ){
            //TODO: Get each posts ID and assign to a variable

            $zipField=get_field('zip_codes_serviced');

        }
        wp_reset_postdata(); 
    } 
}
?>

When I var_dump($posts); I get an array of 37 objects 1 for each of the location post types. I included one of the objects below:

object(WP_Post)#1758 (24) { ["ID"]=> int(1490) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-09-21 15:39:29" ["post_date_gmt"]=> string(19) "2018-09-21 15:39:29" ["post_content"]=> string(0) "" ["post_title"]=> string(14) "Scottsdale, AZ" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(13) "scottsdale-az" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2018-09-21 20:54:47" ["post_modified_gmt"]=> string(19) "2018-09-21 20:54:47" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "http://dev-site.host.my/?post_type=location&p=1490" ["menu_order"]=> int(0) ["post_type"]=> string(8) "location" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" }

So I know that part of it works. I tried using the get_metadata() function but that does not seem to give me the information I need. Does anyone have any experience with this?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题