How to display CPT in WordPress using WP_Query

By 4 years ago

Sometime it is required to display data from Custom Post Type in PHP template file in WordPress. In my previous tutorial post, you have read How to create custom post type in WordPress using CPT UI. In this tutorial, you will see How to display CPT in WordPress using WP_Query.

In my previous tutorial, I have created a custom post type testimonial_cpt labelled as “Testimonials”. In this tutorial I am going to fetch the data from this testimonial_cpt. If you are beginner, then I will recommend to read my previous tutorial.

How to display CPT in WordPress

Creating Custom Page template

Let’s create a custom page template.

Create a PHP file with any name say testimonial.php. Add the following comment lines at the top of this template file.

<?php
    /**
     **Template Name: Testimonial
     */

Adding WP_Query

Adding HTML to display on web page.

The above code display the simple list of title & description created in testimonial post type in previous article.

Here is the full source code:

Create a page in WordPress Page from dashboard and assign the above created template file from page attribute section. Now, publish and view the page.

View Comments

*

Share
Rahul Kumar

Senior Manager at iB Arts Pvt. Ltd. Experienced Manager with a demonstrated history of working in the IT industry. Skilled in PHP, WordPress, Laravel, jQuery, Flutter.