admin管理员组文章数量:1305418
I am trying to output related posts of the current post by tag_ID
. With the current code, posts will output all posts from the property
tag instead of specific tag.
How can I only return posts based on the current posts tag_ID
?
<?php $post_tag = get_the_tags($post->ID)?>//Not sure if correct
<?php
$args = array(
'post_type' => 'property',
'tag' => $post_tag,
);
$related_posts = new WP_Query( $args );
?>
<?php while ( $related_posts -> have_posts() ) : $related_posts -> the_post(); ?>
<h2><?php echo get_the_title()?></h2>
//etc
<?php endwhile; wp_reset_query(); ?>
Solution:
Might not be the best solution but manages to query related posts that are within city
and the current posts tag.
$tags = wp_get_post_terms( get_queried_object_id(), 'city', ['fields' => 'ids'] );
// Now pass the IDs to tag__in
$args = array(
'post_type' => 'property',
'post__not_in' => array( $post->ID ),
'tax_query' => array(
array(
'taxonomy' => 'city',
'terms' => $tags,
),
),
);
$related_posts = new WP_Query( $args );
I am trying to output related posts of the current post by tag_ID
. With the current code, posts will output all posts from the property
tag instead of specific tag.
How can I only return posts based on the current posts tag_ID
?
<?php $post_tag = get_the_tags($post->ID)?>//Not sure if correct
<?php
$args = array(
'post_type' => 'property',
'tag' => $post_tag,
);
$related_posts = new WP_Query( $args );
?>
<?php while ( $related_posts -> have_posts() ) : $related_posts -> the_post(); ?>
<h2><?php echo get_the_title()?></h2>
//etc
<?php endwhile; wp_reset_query(); ?>
Solution:
Might not be the best solution but manages to query related posts that are within city
and the current posts tag.
$tags = wp_get_post_terms( get_queried_object_id(), 'city', ['fields' => 'ids'] );
// Now pass the IDs to tag__in
$args = array(
'post_type' => 'property',
'post__not_in' => array( $post->ID ),
'tax_query' => array(
array(
'taxonomy' => 'city',
'terms' => $tags,
),
),
);
$related_posts = new WP_Query( $args );
Share
Improve this question
edited Aug 26, 2017 at 18:43
scopeak
asked Aug 25, 2017 at 17:03
scopeakscopeak
2131 gold badge5 silver badges15 bronze badges
2 Answers
Reset to default 2get_the_tags()
returns an array of tags name, ID, and more. You should store only IDs in an array, and use it in your query.
$post_tag = get_the_tags ( $post->ID );
// Define an empty array
$ids = array();
// Check if the post has any tags
if ( $post_tag ) {
foreach ( $post_tag as $tag ) {
$ids[] = $tag->term_id;
}
}
// Now pass the IDs to tag__in
$args = array(
'post_type' => 'property',
'tag__in' => $ids,
);
// Now proceed with the rest of your query
$related_posts = new WP_Query( $args );
Also, use wp_reset_postdata();
instead of wp_reset_query();
when you are using WP_Query();
.
UPDATE
As pointed out by @birgire, WordPress offers the wp_list_plunk()
function to extract a certain field of each object in a row, which has the same functionality as array_column()
function.
So, we can change this:
// Define an empty array
$ids = array();
// Check if the post has any tags
if ( $post_tag ) {
foreach ( $post_tag as $tag ) {
$ids[] = $tag->term_id;
}
}
To this:
// Check if the post has any tags
if ( $post_tag ) {
$ids = wp_list_pluck( $post_tag, 'term_id' );
}
While Jack here gives a good example of how the tags can be obtained, I found a shorter way to obtain all tags in one Array.
$tags = wp_get_post_terms($post->ID, 'post_tag', array("fields" => "ids"));
var_dump($tags); // Output: array(3) { [0]=> int(47) [1]=> int(43) [2]=> int(42) }
本文标签: WP Query related posts by current page Tag ID
版权声明:本文标题:WP Query related posts by current page Tag ID 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741789945a2397599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论