繁体   English   中英

WP_Query 调用上的 get_post_meta

[英]get_post_meta on a WP_Query call

我正在使用 WP_Query 调用从我的 WordPress 网站获取办公室。

$offices = new WP_Query([
    'post_type' => 'office',
    'post_status' => 'any',
    'posts_per_page' => -1,
]);

这将返回许多结果,例如:

Array (  
    [0] => WP_Post Object  
        (  
            [ID] => 52856  
            [post_author] => 2
        )  
      )

所以现在我想循环调用一个 while 条件来使用get_post_meta获取元数据,但我似乎无法弄清楚。

这是我到目前为止所拥有的:

foreach ($offices as $office) {
    while ($offices->have_posts()) {
        get_post_meta($office, 'id', $office->id);
    }
}

这似乎只是导致无限循环被卡住。

第二个循环似乎是多余的。

foreach ($offices as $office) {
   $postMetaArray[$office->ID] = get_post_meta($office->ID, '', false);
}

应该为您提供一个以 ID 为键的所有元数据的数组。

是的,这是一个无限循环。

foreach ($offices as $office) {
    while ($offices->have_posts()) {
        get_post_meta($office, 'id', $office->id);
    }
}

foreach 循环没有尽头,因为 $office 没有做任何事情。 您只需要 while 循环。 并且 arguments 的顺序不正确get_post_meta

while ($offices->have_posts()) {

    get_post_meta( int $post_id, string $key = '', bool $single = false );

}

暂无
暂无

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

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