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条回答 默认 最新

  • dseigqk7443 2018-10-23 22:07
    关注

    ACF's get_field function takes a Post ID as a second parameter:

    $zipField = get_field('zip_codes_serviced', $post->ID);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?