wp query – Exclude a specific post in a Custom Post Type


I have a very simple query to display all my posts in a custom post type:

    $page = get_page_by_title('Duck');
    $args = array(
        'post_type' => 'sessions',
        'post__not_in' => array($page->ID)
    $query = new WP_Query( $args );

<?php if ( $query->have_posts() ) { ?>
        <?php while ( $query->have_posts() ) { $query->the_post(); ?>
                <?php the_title(); ?>
                <?php the_content(); ?>
        <?php } ?>
<?php } ?>

<?php wp_reset_postdata(); ?>

What I want to do is display all the posts except the "Duck" post, I’m getting it’s ID by it’s name – which normally isn’t a problem, but the query still shows this post. I’m guessing I’m just not going about this the right way.

Can anyone help me out here, I was also thinking exclude > $page->IDor something like that – which is what I’d do in a regular loop…just not sure how to do it in a Custom Post Type.


Josh Rodgers 1 year 2021-09-11T20:50:32-05:00 0 Answers 0 views 0

Leave an answer