How to Create Shortcode for Custom Post Type to Display Posts

Creating a shortcode for custom post types is always a good choice, no matter if you are working on a custom WordPress theme or building a plugin.

But, before jumping into creating a shortcode for the custom post type, let’s first create a custom post type to make it more clear and easy to understand, and then will create a shortcode for this.

Sounds good?… Let’s do this.

1. Create a Custom Post Type

At this point, I am assuming you must be aware of what is custom post type and how to create them.

So here, I am creating a Custom Post Type named “Movies”. Copy the code below and paste it in your theme’s functions.php file or in your plugin’s main file.

// First create a custom post type name 'Movies'
function diwp_movies_custom_post_type(){
    // Set labels for the custom post type
    $labels = array(
                     'name' => 'Movies',
                     'singular_name' => 'Movie',
                     'add_new'    => 'Add Movie',
                     'add_new_item' => 'Enter Movie Details',
                     'all_items' => 'All Movies',
                     'featured_image' => 'Add Poster Image',
                     'set_featured_image' => 'Set Poster Image',
                     'remove_featured_image' => 'Remove Poster Image'
    // Set Options for this custom post type;
    $args = array(    
                    'public' => true,
                    'label'       => 'Movies',
                    'labels'      => $labels,
                    'description' => 'Movies is a collection of movies and their info',
                    'menu_icon'      => 'dashicons-video-alt2',    
                    'supports'   => array( 'title', 'editor', 'thumbnail'),
                    'capability_type' => 'page',
    register_post_type('movies', $args);
add_action( 'init', 'diwp_movies_custom_post_type' );
// Custom Post Type ends here.

After adding this code in your theme or plugin’s file, look into your WordPress admin area and you will see a custom post type “Movies” listed there. As shown in the image below.


Note: Add some posts as data in this custom post “Movies” to see the results.

So far, you have successfully created a custom post type called “Movies”. Now let’s create a shortcode for this custom post type to display its posts.

2. Create Shortcode for Custom Post Type

I have written a detailed article about creating different types of shortcodes with parameters, you can check that to get a better idea about creating shortcodes and understanding of the code given below.

So in this case, my custom post type is “Movies” and I will create a shortcode, that will display the movies list.

Again, copy this code and paste it into your theme’s functions.php file or in your plugin’s specific file.

// >> Create Shortcode to Display Movies Post Types
function diwp_create_shortcode_movies_post_type(){
    $args = array(
                    'post_type'      => 'movies',
                    'posts_per_page' => '10',
                    'publish_status' => 'published',
    $query = new WP_Query($args);
    if($query->have_posts()) :
        while($query->have_posts()) :
            $query->the_post() ;
        $result .= '<div class="movie-item">';
        $result .= '<div class="movie-poster">' . get_the_post_thumbnail() . '</div>';
        $result .= '<div class="movie-name">' . get_the_title() . '</div>';
        $result .= '<div class="movie-desc">' . get_the_content() . '</div>'; 
        $result .= '</div>';
    return $result;            
add_shortcode( 'movies-list', 'diwp_create_shortcode_movies_post_type' ); 
// shortcode code ends here

By using the above code, I have created a shortcode named [movie-list] and this code will display the 10 recent movie info as a list.

Note: In the above code, for designing purpose and make things better aligned, I have used some CSS classes. You can find those CSS classes below. However, you can create your own CSS classes as well to make it fit as per your designs.

/* CSS Classes to use */

 width: 100%;
 margin:0 auto;	
 clear: both;
 margin-bottom: 20px; 
 overflow: auto;
 border-bottom: #eee thin solid;
 padding-bottom: 20px;  

width: 160px;
float: left;
margin-right: 25px; 

.movie-poster img{
width: 100%;
height: auto;

font-size: 35px;

3. Use Shortcode to Display Posts From Custom Post Type

Add the shortcode [movie-list] on the page/post or wherever you want to display the list of movies.

After adding that shortcode, you will see a list of movies with their info as shown in the image below as a result.


Note: Your result could be different from my results or shown in the image below, but yes this is the way to create a shortcode for custom post type and display posts from that custom post types by using shortcode.

That’s it! You’re done.

8 thoughts on “How to Create Shortcode for Custom Post Type to Display Posts”

    1. Bhuvnesh Shrivastava

      Hi Pedro,

      Yes, you can modify the arguments for your WordPress query and in the loop to get the list of categories or taxonomies of your post types. Let me know if you face any issue or need help with that.


  1. Clean and clear explanation. Thumbs up. My custom post types do not have the content section filled out. Instead i would like to display the text from a specific field or fields. How would I go about doing that?

    1. Bhuvnesh Shrivastava

      Hi David,

      You can get the value of that specific field by get_post_meta(), and display the value inside the content that you want to display as result of your shortcode. If you are using ACF for custom field then you can use get_field() function.

      Hope this will help you, if you still face any issue then let me know, I will explain it to you.


  2. Hi Bhuvnesh,

    This is a nice post that you’ve written. I was wondering, if you can tell me how to make a shortcode for a previously existing custom post type which is part of a theme. I think, I’ll have to fiddle with the child theme, right?

    1. Bhuvnesh Shrivastava

      Hello Mukul,

      You can create shortcode for existing post type by passing the post type name in the post type argument for your shortcode. To do that, you have to find your existing custom post type name and then pass it in the argument.

      1. To find the name of your post type, just click on that post type and see the post_type parameter value in the URL box. As showing in the example here.
      2. Now you can use this post type name in your shortcode post_type parameter, as given below:
       $args = array(
                          'post_type'      => 'bd-gallery',
                          // rest of the arguments
      // rest of the code for shortcode

      Hope this help you to create shortcode for existing post type, If you still have query feel free to ask and keep Visiting DIVE IN WP.


Comments are closed.