最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WP Query related posts by current page Tag ID

programmeradmin0浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 2

get_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) }
发布评论

评论列表(0)

  1. 暂无评论