根据 ACF 关系字段显示“相关帖子”
Posted
技术标签:
【中文标题】根据 ACF 关系字段显示“相关帖子”【英文标题】:Display "Related Posts" based on ACF Relationship Field 【发布时间】:2016-11-04 00:33:15 【问题描述】:我想显示单个帖子页面的“相关帖子”,其中包含名为“属性”的自定义帖子类型,该帖子类型将 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'
// )
)
));
【问题讨论】:
谢谢,更新了。 【参考方案1】:事实证明,这是由于 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'
)
)
));
【讨论】:
【参考方案2】:一个更复杂的查询,带有一个称为 medienform 的附加分类法
// Shortcode for ACF - Add Relationship ACF field [sc_acf_fields]
//
add_shortcode( 'sc_acf_fields', 'related_relationship' ); // Add your shortcode here
function related_relationship()
// get the taxonomy medienform
$cat_taxonomies = get_terms( 'medienform', $args );
$count = count($cat_taxonomies); // wird nicht verwendet
//
/// foreach category as $form_category - medienform
//
foreach ( $cat_taxonomies as $form_category ):
// check the arguments of the post_type, orderby
$args =array(
'posts_per_page' => -1,
'post_type' => 'materialien', // the CPT
'orderby' => 'title', // menu_order
'order' => 'ASC', // DESC
'tax_query' => array( // the tax_query is important to list the posttypes to its taxonomies
'relation' => 'AND',
array(
'taxonomy' => 'medienform',
'field' => 'slug',
'terms' => $form_category->slug
)
),
'meta_query' => array( // the meta_query is important to list only the related posts of the key
array(
'key' => 'post-relationship', // name of custom field Relationship in: https://qualitaetsoffensive-teilhabe.de/wp-admin/post.php?post=12067&action=edit&classic-editor=1
'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
),
);
//
// make an own WP_Query from the $args
$materials = new WP_Query( $args );
//
// let´s give the category ->name here and the term_link().
// actually a category should only be displayed when it has some child // here we build the accordeon
//
if ($materials->have_posts() )
echo '<h4><a href="' . get_term_link( $form_category ) . '">' . $form_category->name . '</a></h4>';
;
//
// while schleife
while ( $materials->have_posts() )
//
//// use variable as the_post() query
$materials->the_post();
?>
<div class="post-loop-single">
<div class="thumbnail-img"><a href="<?php the_permalink(); ?>"><span><?php the_post_thumbnail('post-thumb'); ?> </span></a></div>
<div class="post-loop">
<div class="post-loop-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
</div>
</div>
<?php wp_reset_query();
endforeach;
【讨论】:
以上是关于根据 ACF 关系字段显示“相关帖子”的主要内容,如果未能解决你的问题,请参考以下文章