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
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.
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.
|
1 |
<?php get_post_types( $args, $output, $operator ) ?> |
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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
function get_custom_post_slugs($att, $content= null) {
extract(shortcode_atts(array(
'custom' => 'no'
), $att));
if (strtolower($custom) =='yes')
{ // Explicity assigning value to array
$args['_builtin'] = false;
}
else
{
$args['_builtin'] = true;
}
$post_types = get_post_types($args,'names');
foreach ($post_types as $post_type )
{ //build the array of custom post slugs
$listTypes[] = '<li>'. $post_type. '</li>';
}
return "\n" . '<ol>' .implode( "\n", $listTypes ). '</ol>' . "\n";
}
add_shortcode('postslugs', 'get_custom_post_slugs'); |
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






