I’m trying to make a custom post return a query and display but instead it is returning and showing the default/standard post. How do I get posts from my CPT?

$query = new WP_Query( array( 'job_posting_type' => 'Job Post' ) );                  

if ( $query->have_posts() ) : ?>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>   
        <div>
            <h2><?php the_title(); ?></h2>
            <?php the_content(); ?>
        </div>
    <?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>
    <!-- show 404 error here -->
<?php endif; ?>

1
1

Assuming your custom post type is named “job_posting”, you just need to change your query to read:

$query = new WP_Query( array( 'post_type' => 'job_posting' ) );

This can be found in the official documentation.

There’s a lot more parameters you might like to use too – you can find a full list in the documentation I linked to. Some I’d recommend considering would be posts_per_page (so you don’t get everything returned at once), and setting post_status to ‘publish’ just in case any draft/private posts get returned (which they shouldn’t anyway, but I like to be safe 😉 ).

Leave a Reply

Your email address will not be published. Required fields are marked *