【发布时间】:2019-11-21 08:46:08
【问题描述】:
我正在尝试一个 Wp_Query,它将获取当前帖子的给定自定义字段的内容,识别与自定义字段内容完全相同的单词的一个标签,然后给我一个也有该标签的其他帖子的列表。例如:Custom_Field 内容为“Paris”。巴黎也是同一帖子(和其他帖子)的标签。我希望查询创建此链接,然后列出更多带有“巴黎”标签的帖子。由于某些标签有多个单词,我宁愿远离 slug 并改用 ID。
$field = get_post_meta($post->ID, 'Custom_Field', true);
$term = term_exists( $field, 'post_tag' );
$term = $term[0]->tag_ID;
使用上述方法(不是说这是正确/最好的方法),我已经能够识别标签并获取它的 ID,但我似乎无法在参数中使用它查询。
'tag_id' => $term
不工作。我已经在多个帖子上对此进行了测试,这些帖子对于给定的自定义字段都有不同的内容,并且每次正确返回与自定义字段的内容匹配的标签 ID。因此,在自定义字段和标签之间建立链接的部分起作用了。我只是无法让该标签的 ID 与查询的参数一起使用。
非常感谢您提供实际解决方案或指向解决该问题的另一篇文章的链接(我一直无法找到)。
编辑:WP_Query 代码:
<?php
global $post;
$field = get_post_meta($post->ID, 'Custom_Field', true);
$term = term_exists( $field, 'post_tag' );
$term = $term[0]->tag_ID;
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'tag_ID',
'terms' => $term
)
),
'posts_per_page' => '5',
'orderby' => 'rand',
'post__not_in' => array(get_the_ID())
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post(); ?>
<h4><a href="<?php the_permalink(); ?>"><?php the_title (); ?></a></h4>
<?php wp_reset_postdata(); ?>
【问题讨论】:
标签: wordpress