Forums

[resolved] Tag archive from a single category - Is this possible? (34 posts)

  1. YoUnGLinKiE
    Member
    Posted 3 years ago #

    I've read this topic through and tried out a few things for my tag page, but it doesn't really work out.

    This is my code:

    <?php get_header(); ?>
    
    <div id="wp_content">
    
    <div id="post_entry">
    
    <?php if (have_posts()) : ?>
    
    <?php
    $current_tag = single_tag_title("", false);
    query_posts(array(
    'cat'=>'8',
    'tag_slug__and'=>array($current_tag),
    ) );
    ?><?php while (have_posts()) : the_post(); ?>
    
    <div class="post_meta" id="post-<?php the_ID(); ?>">
    
    <div class="post_title">
    <div class="post_permalink">
    <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
    <span class="author">Posted By: <?php the_author_posts_link(); ?>&nbsp;<?php edit_post_link(__(' Edit'), '|', ''); ?></span>
    <span class="category">Published in <?php the_category(', ') ?></span>
    <?php if(function_exists("UTW_ShowTagsForCurrentPost")) : ?>
    <span class="category"><?php UTW_ShowTagsForCurrentPost("commalist", array('last'=>' and %taglink%', 'first'=>'tag in %taglink%',)) ?></span>
    <?php endif; ?>
    </div>
    <div class="post_date"><p class="days"><?php the_time('j'); ?></p><p class="month"><?php the_time('M'); ?></p></div>
    </div>
    
    <div class="post_content"><?php the_content("<br />" . "continue reading&nbsp;" . "&quot;" . the_title('', '', false) . "&quot;"); ?></div>
    <div class="post_commented"><a href="<?php comments_link(); ?>"><?php comments_number('no comment','1 comment','% comments'); ?></a></div>
    </div>
    <div class="clearfixed"></div>
    
    <?php endwhile; ?>
    
    <div class="post_updates"><?php if(function_exists('wp_pagenavi')): ?> <?php wp_pagenavi(); ?><?php else : ?><?php posts_nav_link(); ?>
    <?php endif; ?></div>
    
    <?php else: ?>
    <h3>Sorry the tags had been deleted</h3>
    
    <?php endif; ?>
    
    </div>
    <?php get_sidebar(); ?>
    <?php get_optional_sidebar(); ?>
    <?php get_footer(); ?>

    I'm currently using a plugin to exclude certain categories from my pages, but what I actually want is to show all posts posted within one category that only have a link in them to display them above the other normal blogposts with full content, permalinks to the stories etc. When I try showing only a certain category it simply doesn't work and shows all posts with that tag.

    Is there a way to show one block with only the contents of the first posts from one category and underneath it other posts from the same tag from another category? Also, I'm using Simple Tags, could that be a problem?

  2. OmeRoon
    Member
    Posted 3 years ago #

    Simple. You don't.

    With the current code base, tag__in/tag_slug__in and category__in (same as "cat") are utterly incompatible. Period. There is no workaround, there is no fix. Without a large rewrite of the query code, it simply can not be done.

    This is a known problem. It's in the bug tracker, but not really considered high priority at the moment.

    Hmmm... so when I select a category (lets say "News"), it's impossible to display all news containing the tags "sport" OR "politics"? Somehow this feels kinda awkward...

    I also applied your patch btw. but discovered an error in the query as you don't assign any aliases to your tables in the JOINS. This will cause a MySQL error when you select both categories and tags. The solution is to assign some aliases to the tables:

    INNER JOIN wp_term_relationships cwtr ON ( wp_posts.ID = cwtr.object_id )
    INNER JOIN wp_term_taxonomy cwtt ON ( cwtr.term_taxonomy_id = cwtt.term_taxonomy_id
    AND cwtt.taxonomy = 'category'
    AND cwtt.term_id IN (....)

    And the same should be done for tags:

    INNER JOIN wp_term_relationships twtr ON (wp_posts.ID = twtr.object_id)
    INNER JOIN wp_term_taxonomy twtt
    ON (twtr.term_taxonomy_id = twtt.term_taxonomy_id AND twtt.taxonomy = 'post_tag'
    AND twtt.term_id IN (....)

  3. OmeRoon
    Member
    Posted 3 years ago #

    Actually, the fix above seems to be the solution for the whole problem. 8-)

    By adding some table aliases to your fix it should work. I just added it in query.php and it works exactly like it supposed to.

    Code snippets:

    (around line 1020 in WP 2.5.1)

    if ( !empty($q['category__in']) ) {
    	$include_cats = "'" . implode("', '", $q['category__in']) . "'";
    	$join .= " INNER JOIN $wpdb->term_relationships citr ON ($wpdb->posts.ID = citr.object_id) INNER JOIN $wpdb->term_taxonomy citt ON (citr.term_taxonomy_id = citt.term_taxonomy_id AND citt.taxonomy = 'category' AND citt.term_id IN ($include_cats)) ";
    }

    (around line 1099 in WP 2.5.1)

    if ( !empty($q['tag__in']) ) {
    	$join .= " INNER JOIN $wpdb->term_relationships titr ON ($wpdb->posts.ID = titr.object_id) INNER JOIN $wpdb->term_taxonomy titt ON (titr.term_taxonomy_id = titt.term_taxonomy_id AND titt.taxonomy = 'post_tag' AND titt.term_id IN ($include_tags)) ";
    	$reqtag = is_term( $q['tag__in'][0], 'post_tag' );
    	if ( !empty($reqtag) )
    		$q['tag_id'] = $reqtag['term_id'];
    }
    
    if ( !empty($q['tag_slug__in']) ) {
    	$include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'";
    	$join .= " INNER JOIN $wpdb->term_relationships tsitr ON ($wpdb->posts.ID = tsitr.object_id) INNER JOIN $wpdb->term_taxonomy tsitt ON (tsitr.term_taxonomy_id = tsitt.term_taxonomy_id) INNER JOIN $wpdb->terms ON (tsitt.term_id = $wpdb->terms.term_id AND tsitt.taxonomy = 'post_tag' AND $wpdb->terms.slug IN ($include_tags)) ";
    	$reqtag = is_term( $q['tag_slug__in'][0], 'post_tag' );
    	if ( !empty($reqtag) )
    		$q['tag_id'] = $reqtag['term_id'];
    }
  4. zudoku
    Member
    Posted 3 years ago #

    I'm using WP as CMS for Intranet project.
    I need to make a sub-category belong to multiple parents so that when I'm in the sub-category page, I can show content from its various parents. (Imagine a Policy page can show content from various department, and a department can show various relevant sub-categories including policy). The complexity comes when I want to include and exclude some of the categories when querying.

    So one hack that I do is instead using category as parents, I use tag as the "parent category" so that I can do query_posts('cat=1,2,3,4&tag=Parent+Parent&showposts=-1');

    In this way I can do some filter and controlling over the post to be displayed.

Topic Closed

This topic has been closed to new replies.

About this Topic