• Hi there,

    I am trying to fetch posts from a specific category that has a specific tag, in my case I want to fetch all posts from my Server Status category that does not have the tag ‘resolved’ and display it on the site. The code I’m trying can be found below.

    if (have_posts()) : while (have_posts()) : the_post();
    if (!in_category('11')) continue;
    
    $tag = get_the_tags();
    if ($tag) { $tag = $tag[0]; }
    if ($tag->name != "resolved") {
    ?>
    
    <h2>Notice: <?PHP the_title(); ?></h2>
    <?php the_content();
    
    }
    endwhile;
    endif;
    ?>

    Right now that don’t work at all and I wonder what I’ve done wrong.. Any assistance is appreciated.

    Kind Regards,
    dex

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    How many posts do you have it set to display on the Options->Reading page?

    Let’s say is 10 (the default). Your approach loads 10 posts, then skips the ones that don’t match category 11 and the resolved tag.

    Also, your method of checking the resolved tag is odd. What if it’s not the first tag?

    Instead, you probably want to just force your loop to only get the posts you want in the first place. This is unnecessarily difficult right now, hopefully a future version will fix it, but for now… You’d do it like so:

    <?php
    query_posts(array( 'cat'=>'11', 'tag_slug__and'=>array('resolved') ) );
    if (have_posts()) : while (have_posts()) : the_post();
    ?>
    <h2>Notice: <?PHP the_title(); ?></h2>
    <?php the_content();
    }
    endwhile;
    endif;
    ?>
    Thread Starter evodex

    (@evodex)

    Hi Otto42,

    Thanks for the quick reply.

    The option you speak of is set to 10 as you say, I fail to see the relevance though, please elaborate.

    What I want is to display all posts that do not have the tag ‘resolved’ in it. If I am not mistaken the above script will include only posts with that tag.

    And I know my method is a bit odd but I am no programmer, I try to make do with the little knowledge I have.

    Kind Regards,
    dex

    Thread Starter evodex

    (@evodex)

    Hi again,

    Also another issue, I have another query fetching posts on the same page, that one is also only fetching with those rules…

    Kind Regards,
    dex

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    The option you speak of is set to 10 as you say, I fail to see the relevance though, please elaborate.

    If you have it set to 10, then skip them all, you get none. See? You won’t get 10 posts, because 10 was all you got in the first place, and then you skipped some. It doesn’t go back and get more later because you skipped some… 10 is it, that’s all you get.

    Here’s the difference:
    Your approach:
    1. Get 10 posts.
    2. Eliminate the ones I don’t want.
    3. Display the rest.

    My approach:
    1. Get 10 posts that fit my criteria.
    2. Display them all.

    See the difference? Always better to specify your criteria up front. That way, you always know what you’re getting. It’s faster too.

    What I want is to display all posts that do not have the tag ‘resolved’ in it. If I am not mistaken the above script will include only posts with that tag.

    Oh. Okay. Use this instead:
    query_posts(array( 'cat'=>'11', 'tag__not_in'=>array('resolved') ) );

    Also another issue, I have another query fetching posts on the same page, that one is also only fetching with those rules…

    Call wp_reset_query() after the first loop to reset it back to defaults.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘The loop, fetching from specific cat. with specific tag.’ is closed to new replies.