我知道如何获取帖子的ACF字段,但我很难获得这些结果中的关系来返回ACF字段。
示例:
$args = array(
\'post_type\' => \'location\',
\'post__in\' => array(67)
);
$q = new WP_Query($args);
$locations = array();
while($q->have_posts()) : $q->the_post();
$p = get_post(); // gets the basic post details
$f = get_fields(); // gets the ACF details
$m = array_merge((array)$p, (array)$f); // merges all details into one array
array_push($locations, $m);
endwhile;
这表明
Array
(
[0] => Array
(
[ID] => 67
[ACF_Field => This is a custom field/value.
...
[ACF_Relationship_Field] => Array
(
[0] => WP_Post Object
(
[ID] => 133
// there should be another [ACF_Fiel]/value here
...
)
)
)
)
因此,数组的第一级中包含ACF值,但您可以看到第二级中没有。我看着
the ACF Reference 这有助于我达到这一点,但不幸的是,我还没有找到任何能与ACF字段相关的关系。
最合适的回答,由SO网友:chifliiiii 整理而成
您需要循环查找结果并再次调用get\\u fields()。因此,在您的代码之后添加smt,如:
foreach( $locations as $l ) {
//grab post id
$pid = $l[\'ACF_Relationship_Field\'][0]->ID;
// grab fields
$fields = get_fields($pid);
// add them back. As ACF_Relationship_Field is an object I will add it next
// to it, but you can do whatever you prefer
$l[\'ACF_Relationship_Field\'][] = $fields;
}