Get the Custom Post Type Slug

Contact: burt (AT) islandcolors (DOT) com | 415.704.1832

custom-post-type-slug

Get the Custom Post Type Slug

What is a WordPress Custom Post Slug?

The Custom Post Type (CPT) slug is the registered name of a particular Custom Post Type. It is its unique name like an IP number or address. That is it.
It is hard enough to grasp the difference between a WordPress Page and Post but in reality, they are both Post; just different types hence different Post Types.
Looking at your Administration Dashboard left sidebar, a number of the items are just different Post Types hence they each have their own Post Type slug or name.

A Page is a Post?

There is a quick way to find the CPT registered name or slug. It doesn’t work all of the time, but it is a good place to start. Go to your Admin Dashboard

Wordpress Page on Admin Bar

A Page is a Post

and select an item on the left sidebar. In this case we are going to choose Pages. Select the top Pages and the screen of all your  Pages will be displayed.

URL Will Show the Way

Look up at the URL Address bar and you will see  something like below that ends with /edit.php?post_type=page . And you find out that really a page is a post type all along. Now, I am really confused. But, lets forget that and just soldier on.

Post Type is Page

Post Type equals Page

Did You Know?

That WordPress pages, menus, post, attachments, and  revisions are all Post or in WordPress parlance ‘built-in‘  Custom Post Types.

Besides those Custom Post Types, there are the ones that you create or are created when some plugins are installed.

Its is confusing – at least for me. Read more here about WordPress Post Types

For a good overview of how to define and add Custom Post Types to your WordPress installation read Justin Tadlock’s still excellent article Custom Post Types in WordPress

Will Not Work All the Time

The above trick will not work every  time, especially with some plugins that create specialized CPT. There is no sure fire way to find out all the Slug names without having to either search through your plugin  or theme’s code.

Thanks to WordPress, there is an easy way  to accomplish this using  the function get_post_types().

We Need a Shortcode

The Get Post Types function’s $args paramater which is optional, is an array of key value arguments that match against the post types. One of those parameters is ‘_builtin’. If it is a WordPress Custom Post the value is true. If it is a Custom Post then the value is false. We are going to use false to find all of the not built-in Custom Post.

The shortcode is going to be called

[ postslugs ]

. It has one paramater custom=’yes’ or custom=’no’. When custom is yes only the non-Wordpress Custom Post slugs will be displayed. When custom is no only the ‘built-in’ WordPress Custom Post slugs will be shown.

Copy the following into your function.php file after the

The Hard Part is Over

After copying the above code into your function.php file and saving it. Create a draft Post or Page and insert the shortcode

[ postslugs ]

.Next preview the Post or Page and you should have an ordered list of all your registered Custom Post Type slugs – including the WordPress built-in ones.

Now with the Custom Post slug names you can add Striking Theme’s great Shortcode Generator metabox to all your Custom Post Types.

Thanks To:

photos by Philippe Dermine

Trackbacks for this post

  1. Striking Theme – Add a Meta Box to Multiple Custom Post | | Island Colors| Burt Gordon
  2. Add a Meta Box in a Custom Post Type for Striking Theme|Burt Gordon

Leave a Reply

This is A Form

Contact Me

We would love to hear from you! Please fill out this form and we will get in touch with you shortly.