WP Custom Post Type Query In WordPress

WordPress provides a function called WP_Query() to fetch custom post types based on specific criteria.

Here’s an example of how to fetch custom post types with the WP_Query() function:

$args = array(
    'post_type' => 'custom_post_type',
    'posts_per_page' => 10,
    'orderby' => 'date',
    'order' => 'DESC'

$query = new WP_Query($args);

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        // Display post content
} else {
    // No posts found


In the above example, the post_type the parameter is set to the name of the custom post type that you want to fetch. Other parameters are used to specify the number of posts to display, the order in which to display them, and any other filtering criteria.

Once you have defined your query using, WP_Query() you can loop through the results and display the post content as needed. Use the wp_reset_postdata() function to reset the post data after the loop to avoid conflicts with other queries on the page.

Overall, WP_Query() is a powerful and flexible way to retrieve custom post types in WordPress and can be customized to suit your specific needs.

Leave a Reply

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