gpt4 book ai didi

php - 根据ACF关系字段显示 "Related Posts"

转载 作者:行者123 更新时间:2023-11-30 22:16:24 25 4
gpt4 key购买 nike

我想显示单个帖子页面的“相关帖子”,其中包含名为“property”的自定义帖子类型,该帖子类型将 ACF 关系字段用于另一个自定义帖子类型。

另一个帖子类型是“联系人”,在单一属性帖子类型中,关系字段正在调用它。我一直在努力理解ACF's documentation here ,但我无法真正理解为什么我的代码无法正常工作。

我需要根据经纪人显示相关属性。我对SQL 语句和表连接不是很了解。

  $properties = get_posts(array(
'post_type' => 'property', // Page Custom Post Type
'posts_per_page' => 6,
'meta_query' => array(
// 'relation' => 'AND',
// array(
'key' => 'contact', // Field name with 2nd custom post type, 'contact'
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
// )
)
));

最佳答案

原来这是困惑的原因是由于 ACF 存储数组的方式。他们的文档虽然适用于他们的情况,但由于嵌套数组而在我的情况下不适用。

这对我有用。

// This is the start of figuring out the array issue
$contact = get_field( 'contact' );
// This showed me the first array
$contact_array = $contact[0];
// This showed me the ACF array and allowed me to return the ID
$contact_ID = $contact_array->ID;

$properties = get_posts(array(
'post_type' => 'property',
'posts_per_page' => 6,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'contact',
// Had to call the value like this as the array was nested like
// a:2:{i:0;s:3:"123";i:1;s:3:"321";} or something.
'value' => '"' . $contact_ID . '"',
'compare' => 'LIKE'
)
)
));

关于php - 根据ACF关系字段显示 "Related Posts",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38148076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com