0

我试图找到如何从下面函数中的object(WP_Post)#每个数组中提取 ,以便我可以将它分配给一个变量并将其添加到函数的参数$post中。这不是一个完整的功能,我只是用来测试。$postsget_field()var_dump

<?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(); 
    } 
}
?>

当我var_dump($posts);得到每个位置帖子类型的 37 个对象 1 的数组时。我包括以下对象之一:

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" }

所以我知道它的一部分有效。我尝试使用该get_metadata()功能,但这似乎没有给我所需的信息。有人对这个有经验么?

4

1 回答 1

0

ACF 的get_field函数将 Post ID 作为第二个参数:

$zipField = get_field('zip_codes_serviced', $post->ID);
于 2018-10-23T22:07:41.820 回答