How to create a post type on WordPress with PHP in just 5 minutes.

Marc Wag­ner

Octo­ber 30, 2020

4 min read|

Today, I want to show you how easy it is to crea­te your own post type for Word­Press in just 5 minu­tes.

So let’s get right to it.

Create Post Type with PHP #

First, we crea­te a new file in our child the­me and open it. I’ll call the file “WordPressCustomPostType.php” for sim­pli­ci­ty.

Next, we’ll crea­te our class that will be respon­si­ble for mana­ging our new post type. This will look like this:

<?php
if(!defined('ABSPATH')){
	exit();
}

class WordPressCustomPostType{
	public function __construct(){}
}

new WordPressCustomPostType();

Now we need a func­tion that informs Word­Press that we have a new post type that should be loa­ded. For this, we use the func­tion “register_post_type”.

public function wp_registerPostType(){
	$labels = array(
		'name' => __( 'Custom Post Type' )
	);

	$args = array(
		'labels'          => $labels,
		'capability_type' => 'page',
		'show_ui'         => true,
		'supports'        => array( 'title', 'editor', 'thumbnail', 'revisions', 'excerpt' )
	);

	register_post_type( 'my_custom_post_type', $args );
}

Short expl­ana­ti­on: In this tuto­ri­al, we will only deal with the mini­mum infor­ma­ti­on you need to crea­te your own Post Type on Word­Press. If you want to add more details, you can always do so. Useful infor­ma­ti­on can be found direct­ly in the Word­Press Code Refe­rence:

Last but not least, we need to tell Word­Press to run our code once all core func­tions have been loa­ded. For this, we extend the con­s­truc­tor and add a new event:

public function __construct() {
	add_action( 'init', array($this, 'wp_registerPostType' ) );
}

Now save the who­le thing and we can switch to the Word­Press backend to view our new post type.

WordPress Custom Post Type Backend Beispiel
Word­Press Cus­tom Post-Type.

You want to learn more? Then take a look at the Word­Press Code Refe­rence.

Output the data with a WordPress shortcode #

One way to access your data is through short­codes. Once crea­ted, you can reu­se the short­code any­whe­re.

First, we add a new func­tion that crea­tes our out­put. To keep the ins­truc­tions short, we will sim­ply query all the titles of our ent­ries and out­put them one below the other. The code for this looks like this:

public function wp_doShortocde( $atts ) {
	$myCustomPosts = get_posts( array(
			'post_type' => 'my_custom_post_type'
		)
	);

	$listOfTitles = array();
	foreach ( $myCustomPosts as $postItem /** @var WP_Post $postItem */ ) {
		$listOfTitles[] = $postItem->post_title;
	}

	return implode( '<br>', $listOfTitles );
}

In order for Word­Press to call our code, we have to link the who­le thing tog­e­ther, of cour­se. For this the­re is the func­tion “add_shortcode”.

add_shortcode( 'run_my_custom_post_type', array( $this, 'wp_doShortocde' ) );

In our exam­p­le I call our short­code “run_my_custom_post_type” which then calls the func­tion “wp_doShortcode” we crea­ted befo­re. Now we can put the short­code on any page or post as fol­lows:

[run_my_custom_post_type]

I crea­ted a new page for this and ente­red my short­code via the Guten­berg edi­tor.

WordPress Gutenberg Editor Shortcode Custom.
My Cus­tom Short­code

Now quick­ly crea­te a few ent­ries with our Cus­tom Post-Type.

Custom Post Type Einträge hinzugefügt.
Cus­tom Post-Type Backend

Once you are done, you can view the who­le thing in the front­end by cal­ling the page with your short­code. For me the who­le thing looks like this now:

WordPress Shortcode Ausgabe

Useful tips #

A few useful tips to make sure ever­y­thing works out.

  • Use a uni­que pre­fix for your post types to make sure the­re are no com­pli­ca­ti­ons with other plug­ins later.
  • Use a pre­fix for func­tions cal­led by a Word­Press event so you can keep bet­ter track of your code.

Code without shortcode #

<?php
if(!defined('ABSPATH')){
	exit();
}

class WordPressCustomPostType{
	public function __construct(){
		add_action('init', array($this, 'wp_registerPostType'));
	}

	public function wp_registerPostType(){
		$labels = array(
			'name' => __( 'Custom Post Type' )
		);

		$args = array(
			'labels'          => $labels,
			'capability_type' => 'page',
			'show_ui'         => true,
			'supports'        => array( 'title', 'editor', 'thumbnail', 'revisions', 'excerpt' )
		);

		register_post_type( 'my_custom_post_type', $args );
	}
}

new WordPressCustomPostType();

Code with shortcode #

<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit();
}

class WordPressCustomPostType {
	public function __construct() {
		add_action( 'init', array( $this, 'wp_registerPostType' ) );
		add_shortcode( 'run_my_custom_post_type', array( $this, 'wp_doShortocde' ) );
	}

public function wp_doShortocde( $atts ) {
	$myCustomPosts = get_posts( array(
			'post_type' => 'my_custom_post_type'
		)
	);

	$listOfTitles = array();
	foreach ( $myCustomPosts as $postItem /** @var WP_Post $postItem */ ) {
		$listOfTitles[] = $postItem->post_title;
	}

	return implode( '<br>', $listOfTitles );
}

	public function wp_registerPostType() {
		$labels = array(
			'name' => __( 'Custom Post Type' )
		);

		$args = array(
			'labels'          => $labels,
			'capability_type' => 'page',
			'show_ui'         => true,
			'supports'        => array( 'title', 'editor', 'thumbnail', 'revisions', 'excerpt' )
		);

		register_post_type( 'my_custom_post_type', $args );
	}
}

new WordPressCustomPostType();

Summary #

With Word­Press you can quick­ly and easi­ly crea­te your own con­tent types (post types) which you can then mana­ge via Word­Press. Also the out­put via a short­code is done quick­ly.

I hope this litt­le con­tri­bu­ti­on has hel­ped you. Of cour­se, I am always hap­py to recei­ve feed­back on indi­vi­du­al topics or sug­ges­ti­ons.

88e86fcb816eff22bc917094df2862d8dd5c0e978b333e6dd5f36f808990c261 96

Arti­kel von:

Marc Wag­ner

Hi Marc here. I’m the foun­der of Forge12 Inter­ac­ti­ve and have been pas­sio­na­te about buil­ding web­sites, online stores, appli­ca­ti­ons and SaaS solu­ti­ons for busi­nesses for over 20 years. Befo­re foun­ding the com­pa­ny, I alre­a­dy work­ed in publicly lis­ted com­pa­nies and acqui­red all kinds of know­ledge. Now I want to pass this know­ledge on to my cus­to­mers.

Hast du eine Fra­ge? Hin­ter­lass bit­te einen Kom­men­tar