WordPress.org

Ready to get started?Download WordPress

Forums

Exclude most recent posts from only 1 category (10 posts)

  1. jamesmoriarty
    Member
    Posted 3 years ago #

    Hi guys,
    This is my problem. On a page I to want to return posts from all categories.

    That is easy enough.

    But I want to exclude the 4 most recent posts from category with id '15'.

    I am not sure how to go about this.

    This is what I have:

    <?php
    $loop = new WP_Query
    ( array( 'post_type' => 'sport', 'cat' => '-15', 'posts_per_page' => 10 ) );
    while ( $loop->have_posts() ) : $loop->the_post();
    ?>

    This of course excludes all of category 15!

    I have experimented with the 'offset' parameter, but with no success.

    Can anybody help? Thanks.

    James

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    You're probably better off dropping the custom query and just setting up a counter and an in_category() conditional to skip the first 4 posts from cat 15.

    <?php if (have_posts()) :
    $c=0;
    while (have_posts()) : the_post();
    if( in_category('15) ) $c++;
    if(  in_category('15) && $c < 5 ) skip;
    ?>
  3. jamesmoriarty
    Member
    Posted 3 years ago #

    Hi esmi,
    Thanks very much for your reply, but I should have included all the code that I am using. This query is outside the loop, as the main loop is in action elsewhere on the page.

    If I use the code you suggest, it upsets the conditional statement below, which basically applies the style 'on' to the post if it appears elsewhere on the page.

    <?php
    $loop = new WP_Query
    ( array( 'post_type' => 'sport', 'cat' => '-15', 'posts_per_page' => 10 ) );
    while ( $loop->have_posts() ) : $loop->the_post();?>
    <li<?php if
    ( $post->ID == $wp_query->post->ID )
    { echo ' class="on"'; } else {} ?>></li>
    <?php endwhile; ?>

    Any ideas as to how to make your suggested code work with this?

    Thanks a million,
    James

  4. esmi
    Forum Moderator
    Posted 3 years ago #

    <?php $c=0;
    $loop = new WP_Query
    ( array( 'post_type' => 'sport', 'cat' => '-15', 'posts_per_page' => 10 ) );
    while ( $loop->have_posts() ) : $loop->the_post();
    if( in_category('15) ) $c++;
    if(  in_category('15) && $c < 5 ) skip;?>
    <li<?php if
    ( $post->ID == $wp_query->post->ID )
    { echo ' class="on"'; } else {} ?>></li>
    <?php endwhile; ?>
  5. jamesmoriarty
    Member
    Posted 3 years ago #

    Thanks, but it doesn't work for some reason. It is including all posts from category 15, and isn't excluding any.

    This is what I have:

    <?php $c=0;
    $loop = new WP_Query
    ( array( 'post_type' => 'sport','posts_per_page' => 10 ) );
    while ( $loop->have_posts() ) : $loop->the_post();
    if( in_category('15') ) $c++;
    if(  in_category('15') && $c < 5 ) skip;?>
    <?php endwhile; ?>

    Any ideas what may be missing?
    Thanks.

  6. esmi
    Forum Moderator
    Posted 3 years ago #

    What do you have between if( in_category('15') && $c < 5 ) skip;?> and <?php endwhile; ?>? Is this Loop replacing the standard Loop?

  7. jamesmoriarty
    Member
    Posted 3 years ago #

    This is in addition to the standard loop, which is further down the page.

    This is what I have in full:

    <?php $c=0;
    $loop = new WP_Query
    ( array( 'post_type' => 'sport','posts_per_page' => 10 ) );
    while ( $loop->have_posts() ) : $loop->the_post();?>
    if( in_category('15') ) $c++;
    if(  in_category('15') && $c < 5 ) skip;?>
    <li<?php if ( $post->ID == $wp_query->post->ID ) { echo ' class="on"'; } else {} ?>>
    <a href="<?php the_permalink()?>">
    <div class="green">
    <?php echo get_post_meta($post->ID, 'sport_other', true);?>:</div>
    </a></li>
    <?php endwhile; ?>

    This is the main loop further down the page:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php endwhile; endif; ?>

    I also have another wp_query before the wp_query above which is as follows:

    <?php $loop = new WP_Query
    ( array( 'post_type' => 'sport', 'cat' => '15', 'posts_per_page' => 4 ) );
    while ( $loop->have_posts() ) : $loop->the_post();?>

    The above are the 4 posts I am trying to exclude from the second loop!

    Thanks Esmi for your help.

  8. esmi
    Forum Moderator
    Posted 3 years ago #

    Just a hunch but I think it's not working because you're using $loop->the_post() instead of $post(). Try using in_category('15', $post->ID) instead and see if that helps.

  9. jamesmoriarty
    Member
    Posted 3 years ago #

    Tried that, but still not working!

    I also changed 'loop' to 'my_query' as follows:

    <?php $c=0; $my_query = new WP_Query
    ( array( 'post_type' => 'sport','posts_per_page' => 10 ) );
    while ( $my_query->have_posts() ) : $my_query->the_post();
    if( in_category('15', $post->ID) ) $c++;
    if( in_category('15', $post->ID) && $c < 5 ) skip;?>
    <?php endwhile; ?>
  10. esmi
    Forum Moderator
    Posted 3 years ago #

    Oh... sorry, sorry, sorry. It's me. where I got skip from, I don't know! Try replacing skip; with continue;

Topic Closed

This topic has been closed to new replies.

About this Topic