How to Create a Custom Post Type in WordPress (Plugin + Shortcode)
Custom post types let you store things like projects, services, team members or events separately from normal posts. In this tutorial you will build a small plugin that registers a Projects post type and a [latest_projects] shortcode that lists them anywhere on your site.
Step 1: Create the plugin file
Create a folder wp-content/plugins/my-projects/ and inside it a file my-projects.php with this code:
<?php
/**
* Plugin Name: My Projects
* Description: Adds a "Projects" custom post type and a [latest_projects] shortcode.
* Version: 1.0.0
* Author: Your Name
* Text Domain: my-projects
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Stop direct access to this file.
}
function myplugin_register_project_cpt() {
register_post_type( 'myplugin_project', array(
'labels' => array(
'name' => __( 'Projects', 'my-projects' ),
'singular_name' => __( 'Project', 'my-projects' ),
'add_new_item' => __( 'Add New Project', 'my-projects' ),
),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'projects' ),
'menu_icon' => 'dashicons-portfolio',
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
'show_in_rest' => true, // Enables the block editor and the REST API.
) );
}
add_action( 'init', 'myplugin_register_project_cpt' );The header comment tells WordPress this file is a plugin. register_post_type() must run on the init hook.
Step 2: Refresh permalinks on activation
Without this, the new /projects/ URLs return a 404 until permalinks are saved manually. Add it below the first function:
register_activation_hook( __FILE__, function () {
myplugin_register_project_cpt();
flush_rewrite_rules();
} );
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );Flushing rewrite rules is slow, so do it only on activation and deactivation, never on every page load.
Step 3: Add a shortcode to list projects
Now add a shortcode so editors can show the latest projects on any page with [latest_projects count="3"]:
function myplugin_latest_projects( $atts ) {
$atts = shortcode_atts( array( 'count' => 6 ), $atts, 'latest_projects' );
$query = new WP_Query( array(
'post_type' => 'myplugin_project',
'posts_per_page' => absint( $atts['count'] ),
'no_found_rows' => true, // Faster: we do not need pagination.
) );
if ( ! $query->have_posts() ) {
return '';
}
ob_start();
echo '<ul class="my-projects">';
while ( $query->have_posts() ) {
$query->the_post();
printf(
'<li><a href="%s">%s</a></li>',
esc_url( get_permalink() ),
esc_html( get_the_title() )
);
}
echo '</ul>';
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode( 'latest_projects', 'myplugin_latest_projects' );Every value printed to the page is escaped with esc_url() and esc_html(), and the shortcode returns its output instead of echoing it.
Step 4: Activate and test
- Go to Plugins in the WordPress admin and activate My Projects.
- A new Projects menu appears. Add two or three projects with a featured image.
- Open
/projects/on your site to see the archive page. - Add
[latest_projects count="3"]to any page to list the latest three projects.
Good practices used here
- A unique prefix (
myplugin_) on every function to avoid conflicts with other plugins. - The
ABSPATHcheck so the file cannot be opened directly. - Escaping all output and using
absint()on user-supplied numbers. wp_reset_postdata()after a customWP_Queryso the rest of the page is not affected.
Frequently asked questions
Should a custom post type go in the theme or a plugin?
In a plugin. If you register it in the theme, all your content disappears from the admin when you change themes. A small plugin keeps the content safe and portable.
Why do I get a 404 error on my new custom post type pages?
The rewrite rules have not been refreshed. Flush them once on plugin activation, as in the code above, or visit Settings › Permalinks and click Save.
What does show_in_rest do?
It makes the post type available in the WordPress REST API, which is also required for the block editor (Gutenberg) to edit it.
Need help with your website?
Tell me what you need and receive a free, page-by-page quotation.
Copyright © 2026 Himanshu Bisoyi All Rights Reserved.
Designed by Himanshu