• Hi !

    I’m trying to mage a custom wp_query to return posts with a certain tag from a certain category. Here is my code:

    $bios = new WP_Query();
    
    	$args = 'cat=6870&tag=tag1,tag2&showposts=2';
            $bios->query($args);
    ?>
    <?php if ($bios->have_posts()) : ?>
    ...

    I know I have posts in the category with the right tags… but the result stays empty… any idea why ?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Have you tried throwing the post id to the get_the_tags function? So, something like this:

    <div class="tag_list">tags:
    <?php
        $tags = get_the_tags($post->ID);
        $total_tag_count=0;
        foreach($tags as $tag) {$total_tag_count++;}
        $tcount = 0;
        foreach($tags as $tag)
        {
            $tag_name = $tag->name;
            $tag_slug = $tag->slug;
            echo '<a href="/tag/'.$tag_slug.'" title="'.$tag_name.'">'.$tag_name.'</a>';
            $tcount++;
            if ($tcount < $total_tag_count)
            {
               echo ", ";
            }
        }
    ?>
    </div>

    You could probably clean up the first foreach so you only have to use one, but this should give you a general idea.

    ref. https://www.ads-software.com/support/topic/252955

    I’m thinking that you could create bios-category.php (supposing ‘bios’ is the slug of your category) then modify that to select only those posts with the required tag.

    (before the loop)
    global $query_string;
    query_posts($query_string . “&tag=tag1,tag2”);

    silentgap and kharisma:

    I’m not trying to be a jerk here or anything, but the OP asked about using WP_Query() and tag parameters, and both of you gave him gave him answers that basically have nothing to do with his question at ALL.

    He’s asking about WP_Query(). Not query_posts(). Not get_the_tags().

    I happen to be having the same problem; WP_Query() doesn’t return anything even though I know for a fact that I have posts with that tag.

    Not sure whether this would make any difference but you could switch around and compress the code a little as follows:

    <?php $args = 'cat=6870&tag=tag1,tag2&showposts=2';
    $bios = new WP_Query($args);
    
    if ($bios->have_posts()) : ?>
    ...

    I also wonder if showposts might be causing an issue. It’s depreciated, so you could try posts_per_page instead?

    Also, could you paste the entire loop for $bios, so we can see if that might be causing an issue?

    Try using an array of arugments.

    Eg.

    $args = array( 'category__in => 6870, 'tag__in' => array( 'tag1', 'tag2' ), 'showposts' => 2 );

    Just a note that I think tag__in can only be used with ID numbers and not slugs.

    Yes my mistake, late night, use IDs with tag__in, or just use tag.

    I’m so glad I found this forum! Thank you so much for your answers, SpankMarvin and Mark/t31os.

    At first, I tried using the array of arguments shown above. This resulted in giving me posts that were in the category or with the tags. (meaning I’d get posts that weren’t in the category, as long as they had the tag attached)

    Next, I used SpanMarvin’s code, cutting out $args and this resulted in what I was looking for. The posts that were within the category AND had the specified tag attached.

    <?php $bios = new WP_Query('category_name=category1&tag=tag1');
    
    if ($bios->have_posts()) : ?>;
    ...

    It seems.. at least with WP 3.0.1 that you can use tag__in and give it the slug

    'tag__in' => array('slug-name'),

    i tried it, having the same problem and it worked.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘custom WP_query with tags’ is closed to new replies.