I am having difficulties excluding a specific page from my main index loop in my child theme.

I have the following function found in functions.php that modifies my index.php loop so that it displays pages instead of posts:

add_action( 'pre_get_posts', 'pages_loop_on_index' );
function pages_loop_on_index( $query ) {

if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( 'post_type', array( 'page' ) );
    $query->set( 'posts_per_page', -1 );

So far, so good.

I want to make it so that the loop ignores page ID 34 but I can't get this part to work.

I've tried using :

$query->set( 'category__not_in', '34' );

Still shows up.

I've tried :

$query->set( 'exclude', '34' );

But the page still shows up.

What am I doing wrong?

Here's the code for index.php:

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

    <?php if ( have_posts() ) : ?>

        <?php /* Start the Loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>

                /* Include the Post-Format-specific template for the content.
                 * If you want to override this in a child theme, then include a file
                 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                get_template_part( 'content', get_post_format() );

        <?php endwhile; ?>

        <?php boardwalk_paging_nav(); ?>

    <?php else : ?>

        <?php get_template_part( 'content', 'none' ); ?>

    <?php endif; ?>

    </main><!-- #main -->

1 Answer 1


You need the WP_Query documentation. WP_Query is very powerful, and you can do a lot with it :)

In your case, I don't think exclude is a valid parameter. What you're probably looking for is this:

$query->set( 'post__not_in', array( 34 ) );

Here's the Post & Page Parameters section of the documentation, which covers this and the other parameters you can use.

(Incidentally, the other one you tried, category__not_in, will only exclude posts from category ID 34, not posts/pages with the specific ID 34)

By the way, you should also add an !is_admin() check to your function, otherwise you might find that this page starts to get excluded from display in the admin area as well.

  • 1
    That did the trick. Thanks for pointing me in the right direction!
    – Sweepster
    Jun 30, 2016 at 1:54
  • 1
    @Sweepster You're welcome! Now you can help answer some others too - there's a lot of questions here from people not understanding the parameters for WP_Query :)
    – Tim Malone
    Jun 30, 2016 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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