Php 使用自定义(ACF)字段创建自定义查询以在Wordpress中显示关系数据
我正在尝试使用Custom(ACF)字段创建一个自定义查询,以在Wordpress中显示关系数据。我什么也没看到。我也没有任何错误 我有两种自定义的帖子类型,“town”和“event” “event”post类型有一个名为“town”的自定义关系字段(使用ACF),在这里我可以将事件与特定的城镇关联起来 在显示城镇(single town.php)的页面上,我试图显示该城镇发生的所有事件Php 使用自定义(ACF)字段创建自定义查询以在Wordpress中显示关系数据,php,wordpress,Php,Wordpress,我正在尝试使用Custom(ACF)字段创建一个自定义查询,以在Wordpress中显示关系数据。我什么也没看到。我也没有任何错误 我有两种自定义的帖子类型,“town”和“event” “event”post类型有一个名为“town”的自定义关系字段(使用ACF),在这里我可以将事件与特定的城镇关联起来 在显示城镇(single town.php)的页面上,我试图显示该城镇发生的所有事件 <?php get_header(); ?> <br> /* displ
<?php
get_header();
?>
<br>
/* display the current town, title, feature image, and description */
<?php
while (have_posts()){
the_post();
$title = get_the_title(); /* use this var in custom query below */
?><h2><?php echo $title ?> </H2>
<hr><?php
the_post_thumbnail('large');
the_content();
}?>
<br>
<?php
/*create a custom query to fetch all the events for that town */
$posts = get_posts(array(
'paged'=> get_query_var('paged',25),
'posts_per_page' => 25,
'post_type' => 'event',
'orderby'=> 'title',
'order'=> 'ASC',
'meta_key'=> 'town',
'meta_query'=> array(
array(
'key'=> 'town',
'compare'=> '=',
'value'=> $title
));
));
/* display custom query results */
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif;
wp_reset_postdata();
get_footer();
?>
“关系”字段将数据保存为ID数组,但不保存文章标题。因此,虽然使用WP_查询的第二个示例比较接近,但它不会找到任何匹配项,因为您将该值与当前文章的标题进行比较。与ID进行比较:
$eventQuery = new WP_Query( array(
'posts_per_page' => 25,
'post_type' => 'event',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'town',
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
),
));
那有什么问题?哦,是的,那可能会有帮助。我什么也得不到。甚至一个错误都没有。我会相应地更新我的帖子。做得很好!谢谢你的帮助!
$eventQuery = new WP_Query( array(
'posts_per_page' => 25,
'post_type' => 'event',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'town',
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
),
));