• Resolved Ken

    (@something4ken)


    My current related posts are related by categories. I’d like to make them related by tags.

    Please help.

    So far, I’ve changed the top part like this, but got stuck:

    *Get current post's tags*/
    $post_tags_temp = wp_get_post_tags( $post->ID );
    $post_tags = array();
    if( !empty( $post_tags_temp )){
    	foreach ( $post_tags_temp as $tag) {
    		$post_tags[] = $tag->term_ID;
    	}
    }

    Here’s link to full file:
    https://pastebin.com/9Nye9MHY

Viewing 6 replies - 1 through 6 (of 6 total)
  • Ken

    I used your template to create another function, which is able to fetch related posts based on categories, I think you will be able to use it in a template to achieve what you are trying to.

    Here’s the code

    function get_related_posts_by_taxonomy($args) {
        $defaults = array(
            'post_id' => get_the_ID(),
            'num_posts' => 6,
            'taxonomy' => 'category',
        );
        $args = wp_parse_args($args, $defaults);
    
        /* Get current post's categories */
        $post_cats_temp = get_the_category($post->ID);
        $post_cats = array();
        if (!empty($post_cats_temp)) {
            foreach ($post_cats_temp as $cat) {
                $post_cats[] = $cat->cat_ID;
            }
        }
        unset($post_cats_temp);
        if (empty($post_cats)) {
            return false;
        }
        /* Prepare the query, allow modifying by applying filter */
        $myposts_query = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'post__not_in' => array($args['post_id']),
            'suppress_filters' => false,
            'posts_per_page' => 6,
            'tax_query' => array(
                'taxonomy' => $args['taxonomy'],
                'field' => 'term_id',
                'terms' => $post_cats
            ),
        );
        $myposts = new WP_Query($myposts_query);
        if($myposts->have_posts()){
            return $myposts->posts;
        }
        else{
            return false;
        }
    }
    Thread Starter Ken

    (@something4ken)

    Thanks Gagan. Can you make it fetch related posts based on tags?

    Ken,

    Just use this to fetch the posts

    $related_posts = get_related_posts_by_taxonomy (array('taxonomy'=>'post_tag'));

    Ken

    The function I wrote above is having bugs, use this function instead, I corrected it

    function get_related_posts_by_taxonomy($args) {
        $defaults = array(
            'post_id' => get_the_ID(),
            'num_posts' => 6,
            'taxonomy' => 'category',
        );
        $args = wp_parse_args($args, $defaults);
    
        /* Get current post's categories */
        $post_cats_temp = get_the_terms($args['post_id'], $args['taxonomy']);
        $post_cats = array();
        if (!empty($post_cats_temp)) {
            foreach ($post_cats_temp as $cat) {
                $post_cats[] = $cat->term_id;
            }
        }
        unset($post_cats_temp);
        if (empty($post_cats)) {
            return false;
        }
        /* Prepare the query, allow modifying by applying filter */
        $myposts_query = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'post__not_in' => array($args['post_id']),
            'suppress_filters' => false,
            'posts_per_page' => 6,
            'tax_query' => array(
                array(
                    'taxonomy' => $args['taxonomy'],
                    'terms' => $post_cats
                )),
        );
        $myposts = new WP_Query($myposts_query);
        if ($myposts->have_posts()) {
            return $myposts->posts;
        } else {
            return false;
        }
    }

    This will work with categories, tags and even any custom taxonomies.

    Use this for fetching related posts with same tags

    $related_posts = get_related_posts_by_taxonomy (array('taxonomy'=>'post_tag'));

    Thread Starter Ken

    (@something4ken)

    Great. Thank you ??

    Always welcome ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Related Posts by Tags’ is closed to new replies.