Forums

query_posts: combine category__in with tag__in (2 posts)

  1. IckataNET
    Member
    Posted 2 years ago #

    Hi all,

    I am trying to execute a query with query_posts, combining category__in and tag__in parameters. Well, it returns nothing :) Is it possible to combine these two parameters anyway?

    $post_categories = get_the_category($post->ID);
    $post_tags = get_the_tags($post->ID);

    //print_r($post_tags);
    $tags = array();
    if (is_array($post_tags)){
    foreach($post_tags as $tag) $tags[] = $tag->term_id;
    }

    $categories = array();
    foreach($post_categories as $cat) $categories[] = $cat->cat_ID;
    //print_r($categories);print_r($tags);
    query_posts(array( 'category__in'=>$categories , 'tag__in'=>$tags , 'post__not_in'=>array($post->ID) , 'caller_get_posts'=>1 ));

  2. kapiljain.in
    Member
    Posted 2 years ago #

    Hi,

    Here is the code for displaying related posts based on the current post categories and tags:

    <?php
    //for use in the loop, list 5 post titles related to tags and categories on current post

    //Getting categories of the current posts
    $post_categories = get_the_category($post->ID);
    if ($post_categories) {
    for($i = 0; $i < count($post_categories); $i++){
    $my_categories .= $post_categories[$i]->term_id . ',';
    }
    }

    //Getting tags of the current posts
    $post_tags = wp_get_post_tags($post->ID);
    if ($post_tags) {
    foreach($post_tags as $tag) $my_tags[] = $tag->term_id;
    }

    echo 'Related Posts';
    $args=array(
    'tag__in' => $my_tags,
    'cat' => $my_categories,
    'post__not_in' => array($post->ID),
    'showposts'=>5,
    'caller_get_posts'=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post();
    //Add code for display posts
    endwhile;
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.