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've got a site with a News page (archive for category 'news') and a separate Blog page. When I make a post for the News page I select the category 'news'. So, on the news page you only see the posts marked news, but on the blog page you can see all posts. What I would like to do is preferably show all posts EXCEPT the 'news' posts on the blog page. How do I do this?

<!--post start-->
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                    <!--post start-->
                    <div class="post">
                        <div class="box">
                            <div class="postimgbox">
                                <?php if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) { ?>
                                    <?php the_post_thumbnail(); ?>
                                <?php } else {

                                }
                                ?>
                            </div>
                            <ul class="post_meta">
                                <li class="post_date">&nbsp;&nbsp;<?php echo get_the_time('M, d, Y') ?></li>
                                <li class="post_comment">&nbsp;&nbsp;
                                <?php comments_popup_link('No Comments.', '1 Comment.', '% Comments.'); ?>
                                </li>
                                <br />
                                <li class="posted_by">&nbsp;&nbsp;
                                <?php the_author_posts_link(); ?>
                                </li>
                                <br />
                                <li class="post_category">&nbsp;&nbsp;
                                <?php the_category(', '); ?>
                                </li>
                                <br />
                            </ul>
                        </div>
                        <div class="post_content">
                            <h1 class="post_title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
                            <?php the_title(); ?>
                                </a></h1>
                    <?php the_excerpt(); ?>
                            <a class="read_more" href="<?php the_permalink() ?>"><?php _e('Read More', 'infoway'); ?>&nbsp;<span class="button-tip"></span></a> </div>
                    </div>
                    <!--End Post-->

Above is the code from blog.php, the php file I am trying to edit as it is the template for my blog page, (if it helps)

share|improve this question
add comment

3 Answers

From a plugin or your theme's functions.php file:

function wpse106861_mod_query( $query ) {
    /* are we on the blog page ? */
    if ( $query->is_home() && $query->is_main_query() ) {
        /* get ID of news category */
        $news_id = get_cat_ID( 'news' );
        /* exclude posts in new from query */
        $query->set( 'category__not_in' => array( $news_id ) );
    }
}
add_action( 'pre_get_posts', 'wpse106861_mod_query' );

Update
As for the comment: is_home should return true on any blog index page, regardless of whether that's a static page or your front page. You can check for the front page directly as well anyhow, by slightly altering the conditional:

if ( $query->is_front_page() && $query->is_main_query() )

Related Reading

share|improve this answer
    
When this is put in functons.php I get a server error 500 –  Omar Jul 17 '13 at 15:06
    
Sorry - Can't see why it would. Are you certain that's not due to a mispaste? –  Johannes Pille Jul 17 '13 at 15:34
    
Thanks, I've got it to stop returning error 500 with a syntax change. I am quite new to php, so how do I call the function in the blog.php template? –  Omar Jul 17 '13 at 15:38
    
"blog.php" is not part of the regular WP template hierarchy. This appears rather theme-specific. It's possible, I guess, that you're not even modifying the main query here. You will have to include (the loop from) blog.php in your question. Both answers you got should work with a standard theme regardless of your Blog Index options. –  Johannes Pille Jul 17 '13 at 15:45
    
I've posted the post posting loop from blog.php, you're right, it is from a theme called 'infoway' –  Omar Jul 17 '13 at 15:50
add comment

You can do it with pre_get_posts filter. Do it like this:

function my_exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'category__not_in', array( 1, 1347 ) );  // where 1 and 1347 are IDs of excluded categories
    }
}
add_action( 'pre_get_posts', 'my_exclude_category' );

PS. I assume that this Blog page is set as your posts page.

share|improve this answer
2  
category__not_in parameter is better suit for this needs. –  Eugene Manuilov Jul 17 '13 at 14:55
    
I've added this to functions.php but it doesn't work. Is this because the front page is set to show latest posts? As when I change it to be static in settings this code works. How do I make this work for a non-static front page –  Omar Jul 17 '13 at 14:59
add comment
up vote -2 down vote accepted

Thank you for replying to my question but after trying to do something else I have managed to work out the answer, and what I believe to be the easiest and shortest answer. In the main loop for posting the posts you will see something along the lines of

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

Before this code add in the following line,

<?php query_posts('cat=-4'); ?>

Where 4 is the category id. The - sign means 'not' and is similar to ! in C. This will mean all posts not in category ID 4 will be displayed on that page

share|improve this answer
1  
That might work in your specific case, but, for your own sake, do not get into the habit of using query_posts(). –  Johannes Pille Jul 18 '13 at 11:12
add comment

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.