• I’m passing some arguments to wp_query() that select all posts except for those in a category named “slider.” In other words, I want to exclude posts in the “slider” category, like so:

    $args = array (
    'posts_per_page' => get_option( 'posts_per_page' ),
    'paged' => max( 1, get_query_var( 'paged' )),
    'cat' => '-112', // slider category
    );
    $wp_query = new WP_Query( $args );

    Here’s the strange thing / unexpected behavior: if a post is in the “slider” category, but it’s marked as sticky, it is *not* excluded from the list of selected posts. Is this correct? Or am I doing something wrong? How can I filter out posts in a category, even if they’re sticky?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi saulbaizman,
    Just replace your code with following code and check

    $args = array (
    'posts_per_page' => get_option( 'posts_per_page' ),
    'paged' => max ( 1, get_query_var( 'paged' )),
    'cat' => '-112', // slider category
    'post__not_in' => get_option( 'sticky_posts' )
    );
    $wp_query = new WP_Query( $args );

    Hope it will works.
    Thanks

    Thread Starter Saul Baizman

    (@saulbaizman)

    cedcommerce,

    Thanks for your response. The issue with this code, which I tested, is that all sticky posts are excluded. I only want to exclude sticky and non-sticky posts in a given category.

    Oh! I got your issue now, you can try the following code for your issue:

    $args = array (
    'posts_per_page' => get_option( 'posts_per_page' ),
    'paged' => max ( 1, get_query_var( 'paged' )),
    'cat' => '-112', // slider category
    );
    $wp_query = new WP_Query( $args );
    if($wp_query->have_posts()) :
       while($wp_query->have_posts()) :
           $wp_query->the_post();
          if ( ! is_sticky( get_the_ID() ) ):
           ?>
            // Post listing here(Means the post listing html here)
           <?php
           endif;
       endwhile;
    endif;?>

    Through this code you’ll be able to exclude the sticky and non-sticky post of a specific category.

    Hope this is what you want!

    Sorry there was a mistake in above code, please paste the below one instead of above

    $args = array (
    'posts_per_page' => get_option( 'posts_per_page' ),
    'paged' => max ( 1, get_query_var( 'paged' )),
    'cat' => '-112', // slider category
    );
    $wp_query = new WP_Query( $args );
    if($wp_query->have_posts()) :
       while($wp_query->have_posts()) :
           $wp_query->the_post();
           if ( ! is_sticky( get_the_ID() ) && ! in_array( '112', wp_get_post_categories( get_the_ID() ) ) ):
           ?>
            // Post listing here(Post html)
    
           <?php
              else :
           ?>
             // Post listing here (Post html)
           <?php
           endif;
       endwhile;
    endif;?>

    Through this code you’ll be able to exclude the sticky and non-sticky post of a specific category.

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Excluding post categories, even for sticky posts’ is closed to new replies.