Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My problem is quite simple:

I've got a couple of posts in different categories that have several tags, some of them in common.

When I load a post, in my sidebar (using PHP Code) I want to show a list of all the posts of another category that share the same tags as the one displayed.

I've tried related posts and it doesn't do the trick, because I found no way of adding the 'only this category' condition.

UPDATE. I have tried this

<?php
global $post;
$tagargs = $catargs= '';
$catnames ='';
$tagnames = '';
$tags = get_the_tags($id);

    if ($tags) {
        foreach ($tags as $tag) {
            $tagnames .= $tag->name . ',';
        }
        $tagargs = array(
            'ignore_sticky_posts' => 1,
            'tag' => $tagnames,
            'post__not_in' => array($id),
            'showposts' => $number,
            'orderby' => 'name',
            'order' => 'asc',
            'cat' => '152'
        );
    }

$posts = get_posts($tagargs);
  foreach($posts as $post) :
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
 <?php endforeach; ?>

And I get all the posts in category 152 (the one I want) but that have only in common the first tag. I have figured out how to do the opposite, I mean, first, get the tags of all the posts inside this category (152) and then compare them with those in the current post (because sometimes, the current post have more than two or three), and get those posts that have all the tags in both sides. What do you think?

share|improve this question

1 Answer 1

First, you want to get the tags that the post in question already has. To do this, use <?php wp_get_post_tags( $post_id, $args ) ?>

Then use WP_Query to grab the posts that have the tag in question:

$query = new WP_Query( '$post_id' );

Here is a sample of how it might work by creating a shortcode to put in your sidebar:

**
 * List Posts by tag.
 *
 * By Matt McFarland  10/23/2013
 * Use as a shortcode [posts_by_tag tag_id={TAG ID} or tag_slug = {SLUG}]
 * Available options are in the shortcode_atts below, non-empty is default
 * examples
 *                  [posts_by_tag amount="4" tag="cars"]
 *                  [posts_by_tag amount="6" tag_id="34"]
 *                  [posts_by_tag tag="cars"]  (amount is 6 by default)
 */
 add_shortcode('posts_by_tag','mm_list_posts_by_tag');
 function mm_list_posts_by_tag($atts) {

    ob_start();
    extract( shortcode_atts( array(
        'amount'        => '6',
        'tag'               => '',
        'tag_id'            => '',
        'orderby'       => 'post_date',
        'order'         => 'DESC',
        'key'               => '',
        'meta_key'      => '',
        'post_type'     => 'post',
        'post_status'   => 'publish', 
    ), $atts ) );       
    $args = array(
        'posts_per_page'   => esc_attr($amount),
        'tag'               => esc_attr($tag),
        'tag_id'            => esc_attr($tag_id),
        'orderby'       => esc_attr($orderby),
        'order'         => esc_attr($order),
        'key'               => esc_attr($key),
        'meta_key'      => esc_attr($meta_key),
        'post_type'     => esc_attr($post_type),
        'post_status'   => esc_attr($post_status));
        $tag_posts = new WP_Query($args); ?>
    <?php if ( $tag_posts->have_posts() ) : ?>  
        <div class="taglist-container">
            <ul>
                <?php while ( $tag_posts->have_posts() ) : $tag_posts->the_post(); ?>
                    <li>
                        <strong><a href="<?php the_permalink(); ?>" rel="bookmark"><?php search_title_highlight(); ?></a></strong>
                        <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?>
                    </li>
                <?php endwhile; ?>
            </ul>
        </div>
    <?php endif; 
    $result = ob_get_clean(); 
    return $result;     
}
share|improve this answer
    
First of all thanks for the answer. Second... maybe i'm too rookie on this field, but i don't know where can i create the shortcode (functions.php?) and then how to insert it in the sidebar.In addition, trying to understand it better, where do I specify the category I want to filter my posts by tags? Thanks again. –  Iván Oct 24 '13 at 8:53
    
You're correct that it goes in functions.php. think of it this way, anything you put in functions.php will be accessible anywhere else. Honestly it depends on your theme as to how and where you would insert it into sidebar. That being said, you should be able to get the post_id. I strongly recommend that you read the full codex in regards to the template tags I mentioned earlier. To be honest, I thought I was too rookie too, but then I just read them in and out and tinkered with them to find answers. Best of luck –  docodemore Oct 24 '13 at 22:23
    
Thanks again. After creating the shortcode and making it work, it lists posts of different categories. They have in common with the current post only the first tag so: they should share at least two or three or all, and I should be able to filter those posts only for one category (different from the current post). Any more ideas? –  Iván Oct 25 '13 at 7:45
    
I found the way to filter by my category! the problem now, it still only shows those posts with the first tag in common. Any more ideas? –  Iván Oct 25 '13 at 8:53
    
do a foreach loop that goes through each tag in common then display them in a list? –  docodemore Oct 25 '13 at 20:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.