How To Query Custom Post Type In WordPress

WordPress is a powerful CMS that allows you to create custom post types to better organize your content. In this blog post, we’ll learn how to query custom post types in WordPress and display their content using the WP_Query class.

Step 1: Create your Custom Post Type

If you haven’t already, the first step is to create your custom post type. You can do this by adding the following code to your theme’s functions.php file or using a plugin like Custom Post Type UI.

    function create_custom_post_type() {
        $labels = array(
            'name' => __('Custom Posts'),
            'singular_name' => __('Custom Post')
        );
        $args = array(
            'labels' => $labels,
            'public' => true,
            'has_archive' => true,
            'supports' => array('title', 'editor', 'thumbnail'),
            'menu_icon' => 'dashicons-admin-post',
        );
        register_post_type('custom_post', $args);
    }
    add_action('init', 'create_custom_post_type');
    

This code creates a new custom post type called “Custom Post” and adds support for title, editor, and thumbnail fields.

Step 2: Query Custom Post Type

Now that you have created your custom post type, you can query its content using the WP_Query class. To do this, create a new PHP file for your custom loop or add the following code to an existing file:

    // Arguments for the custom post type query
    $args = array(
        'post_type' => 'custom_post',
        'posts_per_page' => 10,
    );

    // Custom query
    $custom_query = new WP_Query($args);

    // Check if there are any posts
    if ($custom_query->have_posts()) :
        while ($custom_query->have_posts()) : $custom_query->the_post();
            // Your loop content here
        endwhile;

        // Reset post data
        wp_reset_postdata();

    else :
        _e('No custom posts found.', 'textdomain');
    endif;
    

This code creates a new WP_Query object, passing an array of arguments that specifies the custom post type (‘custom_post’) and the number of posts per page (10). Then, it checks if there are any posts and loops through them, displaying their content. Finally, it resets the post data to prevent conflicts with other loops on the page.

Step 3: Display Custom Post Content

Inside the loop, you can display the custom post’s content using the usual WordPress template tags. For example, to display the post title, content, and thumbnail, you would add the following code:

    echo '<article id="post-' . get_the_ID() . '" . post_class>';
    echo '<header class="entry-header">';
    the_title('<h2 class="entry-title"><a href="'%20.%20esc_url(get_permalink())%20.%20'" rel="bookmark">', '</a></h2>');
    echo '</header>';

    echo '<div class="entry-content">';
    the_content();
    echo '</div>';

    if (has_post_thumbnail()) {
        the_post_thumbnail();
    }

    echo '</article>';
    

This code uses the get_the_ID(), post_class(), the_title(), the_content(), and the_post_thumbnail() functions to display the custom post’s content and metadata.

Conclusion

Querying custom post types in WordPress is simple and straightforward using the WP_Query class. With just a few lines of code, you can create a custom loop that displays your custom post type’s content on your website. Happy coding!