WordPress.org

Ready to get started?Download WordPress

Forums

Excluding post categories from my gallery. (5 posts)

  1. jma666
    Member
    Posted 2 years ago #

    I am trying to specify some categories to exclude from a gallery listing different types of post.
    Here is the code I have.

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts( array(
    'category__not_in' => array( 144, 145 ),
    'post_type' => 'gallery',
    'posts_per_page' => -1,
    'ignore_sticky_posts' => 1,
    'orderby' => $gogo_gallery_items_order
    					 )
    			  );
    $postcount = 0;
    if ( have_posts() ) : while ( have_posts() ) : the_post(); $postcount++;?>

    I added this line:
    'category__not_in' => array( 144, 145 ),
    but it doesn't seem to filter the categories I don't want.
    Anybody know what would be the correct solution?
    Thanks!

  2. Andrew Bartel
    Member
    Posted 2 years ago #

    You could just specify which categories you want instead of excluding.

    That said, I have had inconsistent behavior from category__not_in when the posts are in multiple categories, one of which is the one you're trying to exclude.

  3. jma666
    Member
    Posted 2 years ago #

    Thanks for your reply.
    The theme I am using has its own "Gallery" section in the CMS. Therefore, I am assuming the page, by default, displays the 4 categories belonging to this "Gallery" section. That's why I am trying to exclude some of these categories.
    So far, no luck tho...
    What did you mean by "inconsistent behavior"?

  4. Andrew Bartel
    Member
    Posted 2 years ago #

    It doesn't work sometimes as I would have expected. I haven't dug into the actual code and looked line by line, but it's probably working as intended.

  5. lawless
    Member
    Posted 2 years ago #

    Hey jma666, I just ran into a similar problem with category__not_in, 'category__not_in' => array( 144, 145 ) seems to get processed as a string instead of an array, for whatever reason.

    Try putting your exclude IDs into a variable and explode that into an Array for 'category__not_in'. This is how I got it to work in my circumstance.

    Try this example of your code:

    <?php
    $excludes = '144,145';
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts( array(
    'category__not_in' => explode(',', $excludes),
    'post_type' => 'gallery',
    'posts_per_page' => -1,
    'ignore_sticky_posts' => 1,
    'orderby' => $gogo_gallery_items_order
    					 )
    			  );
    $postcount = 0;
    if ( have_posts() ) : while ( have_posts() ) : the_post(); $postcount++;?>

Topic Closed

This topic has been closed to new replies.

About this Topic