Better Practice, Dec. 2018

(singke) #1
4 \ December 2018 \ http://www.phparch.com

Custom Post Types in WordPress

Listing 1 is a simple example of how to register a custom
post type from the WordPress documentation^2.
Listing 2 builds on this idea to create a “Hiking Trails”
custom post type (mycpt_hike) with some additional func-
tionality. I prepended my custom post type name with mycpt_
to prevent clashes in case another plugin creates a hike post
type.
This example leverages WordPress’s custom post type regis-
tration arguments for the following items.

Custom Labels


Naming conventions for different areas of the WP Admin
have been set explicitly to provide clarity for our site’s editors.

Add a Page for Each Post In This Post Type


By setting has_archive to true, posts in this custom post
type will automatically have a page listing them which is
viewable from your website’s frontend.

Hide from WordPress Search
For this example, I have set exclude_from_search to true, so
items of this post type do not appear in the WordPress site
search. It can be helpful to exclude a custom post type from
search if the data is always intended to appear in a certain
context or if you wish to limit your site’s search to focus on
blog posts and other custom post types.

Leverage Built-in WordPress Admin Tools
Defining supports enables the WP Admin editor to include
a title, content block, featured image selector, revision history,
and page attributes which are useful for setting an order for
posts to appear in.

Change the Default Icon In the WP Admin


We can use menu_icon to display a different icon on the WP
Admin rather than the default post “pushpin” icon.


This option may not be essential if you are only creating
one custom post type. However, if you create several custom
post types and they all have the default pushpin icon, it can
be difficult for your site managers to quickly navigate the WP
Admin. Luckily, you can choose from the WordPress dashicon
library to quickly add an icon to represent your custom post
type or even upload your image.
In Figure 1, The image on the left shows what custom post
types look like without defining a custom icon, while the one
on the right features WordPress dashicons to help the admin
find what they’re looking for.
You can view the available dashicon WordPress icons^3
online.
See the WordPress documentation to learn more about the
function reference for register_post_type^4


2 WordPress documentation: https://codex.wordpress.org/Post_Types
3 dashicon WordPress icons: https://phpa.me/wordpress-dashicons


4 register_post_type: https://phpa.me/wp-register-post-type


Nesting Related Custom Post Types In the WP Admin Menu
If you create a sophisticated website which leverages several
custom post types, your WordPress Admin menu can get
long and difficult to navigate, even with custom icons. In this
instance, it can be helpful to nest similar custom post types
inside a parent menu item.
First, when you register your post types you would like to
nest, you should set show_in_menu to false to remove the link
from the WP Admin left sidebar.

register_post_type( 'mycpt_bike',
// (condensed; see full example above)
'public' => true,
'show_in_menu' => false,
);

Listing 1


  1. function create_post_type() {

  2. register_post_type( 'acme_product',

  3. array(

  4. 'labels' => array(

  5. 'name' => __( 'Products' ),

  6. 'singular_name' => __( 'Product' )

  7. ),

  8. 'public' => true,

  9. 'has_archive' => true,

  10. )

  11. );

  12. }

  13. add_action( 'init', 'create_post_type' );


Listing 2


  1. register_post_type( 'mycpt_hike',

  2. array(

  3. 'labels' => array(

  4. 'name' => __( 'Hiking Trails' ),

  5. 'singular_name' => __( 'Hiking Trail' ),

  6. 'add_new_item' => __('Add new trail'),

  7. 'edit_item' => __('Edit trail'),

  8. 'new_item' => __('New trail'),

  9. 'view_item' => __('View trail'),

  10. 'search_items' => __('Search trails'),

  11. 'not_found' => __('No trails were found'),

  12. 'not_found_in_trash' =>

  13. ('No trails found in trash')

  14. ),

  15. 'public' => true,

  16. 'has_archive' => 'true',

  17. 'exclude_from_search' => true,

  18. 'supports' => array(

  19. 'title', 'editor', 'thumbnail',

  20. 'revisions', 'page-attributes'

  21. ),

  22. 'menu_icon' => 'dashicons-location-alt'

  23. )

  24. );

Free download pdf