Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I asked this question without a clue what was going on. I got an answer with the code below. It worked when I got it. I tested it with shirts only, assuming it worked with the other cats. it doesn't work with hoodies. Why.. idk. Gallery still hides from blog post. Any one see why?

<?php
$gallery = get_cat_id('gallery');
$shirts = get_cat_id('shirts');
$hoodies = get_cat_id('hoodies');
$excluded_cats = '-'.$gallery.',-'.$shirts.',-'.$hoodies;
$limit = 5;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( 'cat=' . $excluded_cats . 'showposts=' . $limit . '&paged=' . $paged );
$wp_query->is_category = true; $wp_query->is_home = false;
?>

http://threefourink.com/threefoursite/blog/

I just now also realized that its showing all the posts, not $limit of 5

Any help much appreciated!

share|improve this question
    
It has to do something with the last cat getting called in order. the esecond and first will work, the third will not. Any ideas? –  Nick Nov 30 '11 at 23:22
add comment

4 Answers 4

You have missed a & before showposts in the query. Also showposts is depracated. Try using posts_per_page instead.

<?php
$gallery = get_cat_id('gallery');
$shirts = get_cat_id('shirts');
$hoodies = get_cat_id('hoodies');
$excluded_cats = '-'.$gallery.',-'.$shirts.',-'.$hoodies;
$limit = 5;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( 'cat=' . $excluded_cats . '&posts_per_page=' . $limit . '&paged=' . $paged );
$wp_query->is_category = true; $wp_query->is_home = false;
?>
share|improve this answer
    
I know i should have just asked you. But its not with the showposts, because the shirts and gallery are not showing up(so its working). Its showing the cat of hoodies. So I thought it was something with the spelling of hoodie somewhere in the admin. But I cant see to figure it out. Or why not all of them, not just one. –  Nick Nov 30 '11 at 5:53
    
any reason for setting $wp_query->is_category = true; $wp_query->is_home = false; ? –  Joshua Abenazer Nov 30 '11 at 5:55
    
I found this line of code in a forum post somewhere I cant find now. It had to do with pagination for the next page of post. Something with making it like the archives page.Idontknow? –  Nick Nov 30 '11 at 6:06
    
Ok does the above code solve your issue? If not I am not clear with what exactly is the issue right now? –  Joshua Abenazer Nov 30 '11 at 6:08
    
No. Basically, this is my blog page. I dont want it to show the named categories above. Its showing the hoodie cat. –  Nick Nov 30 '11 at 6:11
show 1 more comment

This is how I exclude categories on my home page:

            <?php if ( is_home() ) { query_posts($query_string . '&cat=-10,-5,-3'); }?>
share|improve this answer
add comment

hey check it in your code unknowingly there might be white spaces else there might be typo error.The above code will works perfect in all the angle.

share|improve this answer
    
Well i figured something out, if I switch the order of the cat in the list above, the last one wont work. For instance, i switched gallery and hoodies around. now the gallery shows up and not the hoodies. –  Nick Nov 30 '11 at 23:14
add comment

You could totally avoid any need to be messing around with the query vars using an action on parse_query instead.

add_action( 'parse_query', 'wpse_35077_cat_excluder' );

function wpse_35077_cat_excluder( $query ) {

    // Empty when it's the post posttype
    if( !empty( $query->query_vars['post_type'] ) )
        return;

    if( is_home() )
        // 1, 2 and 3 represent example category IDs to exclude posts from
        set_query_var('category__not_in', array( 1, 2, 3 ) );
}

If you need it to work for a front page and not the home page, simply replace is_home() with is_front_page().

NOTE: Posts that are stickies ignore category exclusions.

share|improve this answer
    
Thank you for your answer, this is above my head. Im still learning. It makes sense.. kind of lol. –  Nick Nov 30 '11 at 23:15
    
All of WordPress' queries(or the post related ones at least), run through a class called WP_Query, whenever a query is run it calls upon several actions and filters, these hooks as they're otherwise known allow query manipulation by simple functions(known most commonly as callback functions). It's an alternative but convenient way to alter queries without much of a worry that you'll modify or remove bits of the query that you may not actually intend to.. –  t31os Dec 1 '11 at 0:56
    
That said, there's usually always more than a few ways to archieve the same thing in code(as i'm sure you've already come to realise), use the method you feel most comfortable with.. ;) –  t31os Dec 1 '11 at 0:58
    
Yeah, it makes sense. I had a problem calling the calling the cat id#'s which is why i used the code i have now. I would just exclude the cats if i knew the id. I dont know where to get the individual id# for each category. Tell me where i can find that, and ill be in great shape lol. –  Nick Dec 1 '11 at 2:57
    
And thank you for your explanation! –  Nick Dec 1 '11 at 2:57
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.