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(); ?>   
            <h2><?php the_title(); ?></h2>
            <?php the_content(); ?>
    <?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>
    <!-- show 404 error here -->
<?php endif; ?>

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 ;) ).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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