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:

?php
    $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() ) { ?
    ul
        ?php while ( $query-have_posts() ) { $query-the_post(); ?
            li
                ?php the_title(); ?
                ?php the_content(); ?
            /li
        ?php } ?
    /ul
?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.

Thanks,

Josh

Topic exclude loop wp-query custom-post-types Wordpress

Category Web


The problem is get_page_by_title, it defaults to post type page so you will need to tell it to target the correct post_type.

Going by your post type sessions, this is what you need.

get_page_by_title('Duck', OBJECT, 'sessions');

The second argument is the type of output, the default is OBJECT so thats what I used as well

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.