We talked about creating a custom post type in WordPress, but how do you query one? Query refers to accessing some information in the MySQL database and, thanks to PHP, helps generate content dynamically in the form of HTML in the user’s browser. Additionally, WP_Query is a special class WordPress defines that permits developers to access some database data and make it appear on the front-end or back-end. In both cases, you need querying to display some content. Thus, we recommend getting comfortable with the query of posts or pages before using the guide below. We mention some basic stuff before we teach you how to query a custom post type in WordPress.
What is WP_Query? Differences in querying a custom post type
Here’s a straightforward code that represents a wp_query class from the WP_query class page on WordPress Codex:
<?php
$the_query = new WP_Query( $args );
?>
However, before it appears anywhere on your WordPress website, you must add it to the WordPress Loop. An uncomplicated way is this one:
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// No posts were found
}
/* Restore original post information */
wp_reset_postdata();
You can add this to the standard loop. Should you need to make multiple queries, you would have to create multiple loops. The second one will not have a global variable. You can find more about the basic properties, tags, taxonomy, and parameters of the wp_query class under “Properties”, “Tag Parameters”, “Parameters”, “Post & Page Parameters” headlines, based on the things you must show. As mentioned, custom post type is any type that isn’t a post, page, revision, attachment, or navigation menu item. We will now proceed with the theme of the article.
How to use WP_Query to display a custom post type
Based on the query for regular posts, you realize you can do a plethora of things. You can stop at showing the title and providing the URL to the page. Similarly, you may want to display content such as pictures, brief text, or custom fields. To sum up, you can use a query in WordPress to display posts based on parameters, including the custom type. We cannot know what you want to show, however. Luckily, the basic example we provide below, combined with all the tips and tricks in the WordPress Codex guide should be sufficient. We will provide an example after describing the steps required to query a custom WordPress post type:
1. Configure a variable that passes parameters to WP_Query
The first step is to set up a variable that will pass an array of parameters to the WP_Query class. In most cases, you’ll use post_type, and then enter the slug or the ID of the custom post type you decided to query. If you have not created one, use the guide in our introduction to do so.
2. Double-check that a post in question is published
This is an important step since you want to ensure that the custom post type you want to query is not saved as a draft or a pending article. Therefore, it must be published and live, meaning the post_status parameter is published. Unsurprisingly, you can also set it up so that the parameter targets only drafts by setting post_status to draft. All other parameters under the “Post Type Parameters” and “Status Parameters” headlines would work, too, including any, such as:
$query = new WP_Query( array( 'post_type' => 'any' ) );
You can also display several types of posts, including custom ones, like this:
$args = array( 'post_type' => array( 'post', 'page', 'ebook', 'show' ) ); $query = new WP_Query( $args );
3. Configure how many posts will respond to a query
It’s likely that multiple custom post types will be fetched based on your parameters. Luckily, you can limit them to a certain number based on the desired preview. The simplest solution is to use the posts_per_page parameter. We will set this at 7.
4. Add additional parameters and tags
Again, we don’t know what your goal is. You can always refer to the “Parameters” headline in the Codex. Well-known examples is using the author ID, posts by a certain author, several author IDs, or excluding a certain author, in that order:
$query = new WP_Query( array( 'author' => 555 ) );
$query = new WP_Query( array( 'author_name' => 'steven' ) );
$query = new WP_Query( array( 'author' => '4,25,27,34' ) );
$query = new WP_Query( array( 'author' => -14 ) );
Similarly, you can use cat to filter based on category ID, tag to query posts based on the tag slug, s to use a search keyword, and so forth. We also think orderby and order parameters will be essential. You can order posts based on, say, their title, and configure their order to, for example, ascending.
5. Add the query to the WordPress Loop and configure the output
Don’t fret, we will show you how the entire thing looks below. For now, understand that you must add your custom post type query to the WordPress loop. But that’s not all. To avoid errors, you need to ensure the loop is reset after your query. That is because a global $post variable uses a page layout that is programmed to assume only one loop on the page exists. Since you insert a new valuable, unless you reset it via the wp_reset_postdata() function, it will continue using yours rather than the global one.
Furthermore, you can use Template Tags from WordPress Codex to display certain content. For instance, home_url() will show the home URL, the_permalink() would display the post URL, get_the_ID() would fetch the post ID, and so on. We will use the basic ones such as the_title() and the_excerpt() in our example.
WP_Query example for a custom post type in the WordPress Loop
Here’s a simple code that demonstrates querying a custom post type in WordPress and adding it to the loop:
$args = array(
'post_type' => 'ebook',
'post_status' => 'publish',
'posts_per_page' => 7,
'orderby’ => 'title',
'order’ => 'ASC',
'cat' => 'fiction',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print the_title();
the_excerpt();
endwhile;
wp_reset_postdata();
As you can see, we set it up to fetch the ‘ebook’ custom post type and display 7 published posts. It will order them based on title and in ascending order and categorize them based on category ID. You would see this on the front end:
Article Title
Example excerpt of the article.. Read More