【问题标题】:Correct Way to Output ACF Generated Values Using WP_Query() and Foreach()使用 WP_Query() 和 Foreach() 输出 ACF 生成值的正确方法
【发布时间】:2025-12-12 10:05:04
【问题描述】:

我使用高级自定义字段 (ACF) 设置字段和子字段。结构类似于:

Field: Pet Details
- Sub-Field: Pet Name (text)
- Sub-Field: Pet Birthday (date picker)
- Sub-Field: Pet Gender (taxonomy, select)
- Additional... 

我已经能够使用get_field()get_sub_field() 函数在我的自定义帖子类型 (/cpt_slug/post_title) 中使用这些,但是我无法使用默认的 ACF 函数 (get_field()、@ 987654326@ 等)以及显示此信息的其他页面 - 在这种情况下,domain.com/account(woocommerce 我的帐户页面)很好,因为我很乐意使用WP_Query

虽然我在下面有一个工作代码,但它需要我为每个 acf 子字段设置$variable = get_post_meta( $pet->ID, field_sub_field, true )。给定数组结构,

我能想出的最好结果是echo $details['pet_details_pet_name'][0],它输出“Cooper”(正确),但由于我对数组的了解有限,有没有可能“0”变成“1”而不是允许这个工作?我不相信 cpt/acf 会在数组中添加第二个值,但我想听听您的想法。

代码

global $current_user;

$query = new WP_Query ( array ( 
    'post_type' => 'audp_pets', 
    'author' => $current_user->ID, 
    'order' => 'ASC' 
    ) );

$pets = $query->posts;

if ( $pets ) {

    foreach ( $pets as $pet ) {

        var_dump ( $pet );

        ?><hr><?php

        echo "\$pet->post_title = " . $pet->post_title . '<br />';

        ?><hr><?php

        $details = get_post_meta( $pet->ID );

        $pet_name = get_post_meta( $pet->ID, 'pet_details_pet_name', true );
        echo "\$pet_name = " . $pet_name . '<br />';

        ?><hr><?php

        var_dump ( $details );


    }

wp_reset_postdata();

}

输出(来源)

object(WP_Post)#13185 (24) {
  ["ID"]=>
  int(952)
  ["post_author"]=>
  string(1) "1"
  ["post_date"]=>
  string(19) "2020-01-16 16:24:17"
  ["post_date_gmt"]=>
  string(19) "2020-01-16 05:24:17"
  ["post_content"]=>
  string(0) ""
  ["post_title"]=>
  string(6) "AA0AA0"
  ["post_excerpt"]=>
  string(0) ""
  ... (additional fields)
}

(N.B -- $pet->post_title = AA0AA0)

array(56) {
  ["_edit_last"]=>
  array(1) {
    [0]=>
    string(1) "1"
  }
  ["_edit_lock"]=>
  array(1) {
    [0]=>
    string(12) "1579152259:1"
  }
  ["pet_details_pet_name"]=>
  array(1) {
    [0]=>
    string(6) "Cooper"
  }
  ["_pet_details_pet_name"]=>
  array(1) {
    [0]=>
    string(19) "field_5e1189ab705b2"
  }
  ["pet_details_pet_birthday"]=>
  array(1) {
    [0]=>
    string(8) "20130313"
  }
  ... (additional fields)
}

(N.B. $pet_name = Cooper)

提前谢谢你。

【问题讨论】:

  • 你可以把它变成一个普通的the loop if($query-&gt;have_posts()): while($query-&gt;have_posts):the_post(); 等等,然后你把它设置成能够以普通的风格来做。 - 否则,您还可以将帖子 ID 发送到任何 ACF 函数并获取值 - 像这样 get_field('fieldname', $pet-&gt;ID)
  • 如果是转发器,请阅读 ACF 关于转发器的文档,与组和布局等相同。都在 acf 文档advancedcustomfields.com/resources/repeater
  • 谢谢@Stender。循环可以在“页面”上使用,例如“我的帐户”还是特定于“帖子”?几天来我一直在研究如何制作 acf 函数,包括 get_field()get_sub_field()' working on this single-custom-post.php and nothing works. It works fine on my /custom-post-slug/page_title/` 但不在此页面上。此外,我发现许多文章指出,使用 ACF 的请求数量比使用 WP_Query() 的要多。
  • @Stender 我也尝试过循环,但这不起作用。在此页面中,没有任何内容允许我从 ACF 函数(例如 get_field()get_sub_field())返回结果(是的,我包含 get_header() 和 get_footer())。
  • 你在呼应吸气剂对吧?

标签: arrays wordpress foreach advanced-custom-fields


【解决方案1】:

基本上,你可以这样做:

global $current_user;

$args = array ( 
'post_type' => 'audar_pets', 
'author' => $current_user->ID, 
'order' => 'ASC' 
);

$query = new WP_Query( $args );

//Here we set the loop up so we can use it all in the normal way.. 
if($query->have_posts()){
    while ($query->have_posts()) {
        $query->the_post();
        echo get_the_title();
        //Get you fields
        var_dump(get_field('pet_details'));

        if( have_rows('pet_details') ):

            // loop through the rows of data
            while ( have_rows('pet_details') ) : the_row();

                // display a sub field value
                the_sub_field('pet_name');

            endwhile;

        else :

        // no rows found

    endif;
    } 
wp_reset_postdata();
}

但这是猜测您使用的是转发器,它确实是一个子字段,而不仅仅是另一个字段..

您还可以通过在末尾添加帖子 ID 来使用 have_rows 等,就像使用任何 ACF 函数一样。

您可以通过添加 ID $value = get_field( "text_field", 123 ); 从任何页面、帖子、附件等获取任何字段

【讨论】:

  • 这是使用wp_query 循环发布帖子。它确实与您如何获取元数据无关 - 但由于这设置了发布数据,您可以使用 $details = get_post_meta( get_the_ID(), 'pet_details_pet_name' ); var_dump($details) - 如果您有中继器,它可能会作为序列化数组返回?
  • 仅供参考,通过查询监视器运行了两个不同的查询。纯 wp_query 和单个请求是 85 个查询,acf 版本是 100 个(包括核心和主题查询)。但它们的加载时间几乎相同。与 2018 年的一篇帖子的作者交谈,re acf 通过这些查询减慢了网站的速度,他说他们解决了这些问题。它看起来是正确的。再次感谢您的帮助。
最近更新 更多