Display Custom Post Types using WP_Query

Now I show you how to display Custom Post Types using WP_Query. In WordPress, we can display the Custom Post Type posts in various ways, but using WP_Query gives a benefit to customize your entire post listing view. With the help of WP_Query arguments, you can list the posts by post type, post status, post order, posts per page, etc. As you like you imagined as you customize and get the view of the post listing.
Create a Custom Post Type in WordPress
Before you start you required to create the Custom Post Type either by plugin or without a plugin. For that here you have an article, If you already created ignore this.
Display Custom Post Type With Created Custom Template
Once you created or duplicated a archive.php with the name of a archive-resources.php by using FTP, cPanel, or WP File Manager then open the archive-resources.php in your theme folder and use the WP_Query in the template. If you need more clarification about creating or duplicating archive.php check this article.
Use the below code in archive-resources.php to display the Custom Post Type posts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php $args = array( // Arguments for your query. 'post_type' => 'resources', //Replace your Created Custom Post Type 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC', ); // Custom query and passing arguments. $query = new WP_Query( $args ); // Check that we have query results. if ( $query->have_posts() ) { // Start looping over the query results. while ( $query->have_posts() ) { $query->the_post(); ?> <h1><?php the_title(); ?></h1> <p><?php the_content(); ?></p> <?php } } // Restore original post data. wp_reset_postdata(); ?> |
Note: Here we displayed only for the particular custom post type resources. Now you need to replace it with your custom post type name which you created.
Output:
Conclusion:
I hope this article will help you to use WP_Query in the best way and if you have any doubts about this topic let me know in the comment box. If you like this article share it with your friends.
Custom PostCustom Post TypesWithout PluginwordpressWP_Query
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]