WordPress Custom Post Types - Notices and Taxonomies
Key Takeaways
- Customizing admin notices for WordPress Custom Post Types (CPT) can be achieved by using the ‘post_updated_messages’ hook, which allows users to change the default alert message displayed when a post is saved, published, or updated.
- In WordPress, taxonomies serve as a mechanism for grouping posts of any type, including CPTs. Custom taxonomies can be created using the ‘register_taxonomy()’ function, allowing for more specific categorization of CPTs.
- Custom Post Types and taxonomies are powerful WordPress features that allow for more organized and specific grouping of data or post entries beyond the default post and page types.
In my previous post, I introduced Custom Post Types (CPT) and how to create one for your WordPress powered website.
We also took a look at how to customize the various UI labels of a custom post type to make it distinct from the native post and page post types. However, we didn’t cover how to customize the admin notices generated by them.
In this tutorial, I will be covering how to customize these notices and also how to register new taxonomies to a custom post type.
Customizing CPT Admin Notices
Are you familiar with the alert message that is displayed near the top of admin pages for example when a post is saved as draft, published or even when you save the settings of a plugin? This message is what is referred to as an admin notice.
By default, the admin notices displayed when working on a custom post assumes you are dealing with a post post type and therefore, when for example a book post type is updated, the following notice is displayed: Post updated. View post.
You can change the text of these messages easily by using the post_updated_messages hook like so:
add_filter( 'post_updated_messages', 'book_cpt_messages' ); /** * Book CPT updates messages. * * @param array $messages Existing post update messages. * * @return array Amended book CPT notices */ function book_cpt_messages( $messages ) { $post = get_post(); $post_type = get_post_type( $post ); $post_type_object = get_post_type_object( $post_type ); $messages['book'] = array( 0 => '', // Unused. Messages start at index 1. 1 => __( 'Book updated.', 'textdomain' ), 2 => __( 'Custom field updated.', 'textdomain' ), 3 => __( 'Custom field deleted.', 'textdomain' ), 4 => __( 'Book updated.', 'textdomain' ), 5 => isset( $_GET['revision'] ) ? sprintf( __( 'Book restored to revision from %s', 'textdomain' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => __( 'Book published.', 'textdomain' ), 7 => __( 'Book saved.', 'textdomain' ), 8 => __( 'Book submitted.', 'textdomain' ), 9 => sprintf( __( 'Book scheduled for: <strong>%1$s</strong>.', 'textdomain' ), date_i18n( __( 'M j, Y @ G:i', 'textdomain' ), strtotime( $post->post_date ) ) ), 10 => __( 'Book draft updated.', 'textdomain' ) ); if ( $post_type_object->publicly_queryable ) { $permalink = get_permalink( $post->ID ); $view_link = sprintf( ' <a href="%s">%s</a>', esc_url( $permalink ), __( 'View book', 'textdomain' ) ); $messages[ $post_type ][1] .= $view_link; $messages[ $post_type ][6] .= $view_link; $messages[ $post_type ][9] .= $view_link; $preview_permalink = add_query_arg( 'preview', 'true', $permalink ); $preview_link = sprintf( ' <a target="_blank" href="%s">%s</a>', esc_url( $preview_permalink ), __( 'Preview book', 'textdomain' ) ); $messages[ $post_type ][8] .= $preview_link; $messages[ $post_type ][10] .= $preview_link; } return $messages; }
Code Explanation: The code above customizes admin notices generated by a book custom post type.
The $messages multi-dimensional array controls the admin notices displayed by any post type.
To customize the messages of a book custom post type, create an index array containing the various messages as the value of $messages['book'].
The if statement checks if the custom post type is publicly query-able. That is, whether the public argument is set to true while registering the custom post type.
If true, a link to view a post belonging to the CPT is added to the admin notice displayed when it is updated, published or scheduled for publication while a link to preview the post is added when it is submitted for review or a draft is updated.
Custom Taxonomies
In WordPress, a taxonomy is mechanism for grouping posts of any type.
Examples of taxonomies include Category for grouping posts that are related to a given category and Tag which is pretty similar to categories but is more free form. More information on taxonomies is available over at the WordPress Codex.
That being said, we are going to cover how to create custom taxonomies. Let us take the example of creating a book post type, categorizing the book entries using the same categories used for blog posts isn’t ideal.
A real life example is the Easy Digital Downloads plugin that uses a download custom post type for digital product entries with a download_category taxonomy for product categorization.
To create a custom taxonomy, use the register_taxonomy() function and hook it to the init Action like so:
add_filter( 'post_updated_messages', 'book_cpt_messages' ); /** * Book CPT updates messages. * * @param array $messages Existing post update messages. * * @return array Amended book CPT notices */ function book_cpt_messages( $messages ) { $post = get_post(); $post_type = get_post_type( $post ); $post_type_object = get_post_type_object( $post_type ); $messages['book'] = array( 0 => '', // Unused. Messages start at index 1. 1 => __( 'Book updated.', 'textdomain' ), 2 => __( 'Custom field updated.', 'textdomain' ), 3 => __( 'Custom field deleted.', 'textdomain' ), 4 => __( 'Book updated.', 'textdomain' ), 5 => isset( $_GET['revision'] ) ? sprintf( __( 'Book restored to revision from %s', 'textdomain' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => __( 'Book published.', 'textdomain' ), 7 => __( 'Book saved.', 'textdomain' ), 8 => __( 'Book submitted.', 'textdomain' ), 9 => sprintf( __( 'Book scheduled for: <strong>%1$s</strong>.', 'textdomain' ), date_i18n( __( 'M j, Y @ G:i', 'textdomain' ), strtotime( $post->post_date ) ) ), 10 => __( 'Book draft updated.', 'textdomain' ) ); if ( $post_type_object->publicly_queryable ) { $permalink = get_permalink( $post->ID ); $view_link = sprintf( ' <a href="%s">%s</a>', esc_url( $permalink ), __( 'View book', 'textdomain' ) ); $messages[ $post_type ][1] .= $view_link; $messages[ $post_type ][6] .= $view_link; $messages[ $post_type ][9] .= $view_link; $preview_permalink = add_query_arg( 'preview', 'true', $permalink ); $preview_link = sprintf( ' <a target="_blank" href="%s">%s</a>', esc_url( $preview_permalink ), __( 'Preview book', 'textdomain' ) ); $messages[ $post_type ][8] .= $preview_link; $messages[ $post_type ][10] .= $preview_link; } return $messages; }
If you have a book custom post type already, you should see the category taxonomy added to the admin menu and post edit screen.


You can also use register_post_type() for registering custom post types, the register_taxonomy() function also accepts an array of arguments for customizing labels and configuration of a custom taxonomy.
I won’t be explaining the arguments because they are pretty much the same as that of register_post_type() . A list of the arguments and descriptions is available here.
Conclusion
Custom Post Types are a powerful feature of WordPress and useful in grouping data or post entries that don’t fit into a post and page type. The icing on the cake is the ability to further categorize the posts of a custom post type by registering a custom taxonomy.
Do you have a question or contribution? Please use the comments to let us know.
Frequently Asked Questions about WordPress Custom Post Types, Notices, and Taxonomies
How can I create a custom post type in WordPress?
Creating a custom post type in WordPress involves adding a few lines of code to your functions.php file. You’ll need to use the register_post_type() function, which allows you to define a new post type by its labels, supported features, availability and other criteria. Remember to flush your rewrite rules after adding the code by visiting the Permalinks settings page.
What are WordPress taxonomies and how do they work?
Taxonomies in WordPress are a way of grouping posts (or custom post types) together. They come in two forms: categories and tags. Categories are hierarchical and can have child categories, while tags are not hierarchical. You can create custom taxonomies using the register_taxonomy() function.
How can I display custom post type notices in WordPress?
To display custom post type notices, you can use the ‘post_updated_messages’ filter hook. This hook allows you to customize the update messages for any post type. You can add a function to your functions.php file that checks the post type and then sets the appropriate message.
How can I associate a custom taxonomy with a custom post type?
When registering a custom taxonomy with the register_taxonomy() function, you can specify the post types that this taxonomy should be associated with. This is done by passing an array of post type names as the second parameter to the function.
What is the ‘post_updated_messages’ hook in WordPress?
The ‘post_updated_messages’ hook in WordPress allows you to customize the messages that are displayed when a post is updated. This can be particularly useful for custom post types, where you might want to display a different message than the default.
How can I use the ‘get_post_type’ function in WordPress?
The ‘get_post_type’ function in WordPress is used to retrieve the post type of the current post or of a given post. It returns a string on success and false on failure. This function can be useful when you need to perform actions based on the post type.
How can I add custom fields to a custom post type in WordPress?
Custom fields can be added to a custom post type in WordPress by using the ‘add_meta_box’ function. This function allows you to add a new meta box to the post editing screen, where you can input additional information for the post.
How can I display custom post types on my site’s front page?
To display custom post types on your site’s front page, you can modify the main query that WordPress uses to display posts. This can be done by using the ‘pre_get_posts’ action hook and setting the ‘post_type’ parameter to the name of your custom post type.
How can I create a custom taxonomy specific to a custom post type?
When registering a custom taxonomy with the register_taxonomy() function, you can specify the post types that this taxonomy should be associated with. This is done by passing an array of post type names as the second parameter to the function.
How can I use the ‘save_post’ action hook in WordPress?
The ‘save_post’ action hook in WordPress is triggered whenever a post or page is created or updated. It can be used to perform actions such as saving post meta data, sending notifications, or other tasks that should happen after a post is saved.
The above is the detailed content of WordPress Custom Post Types - Notices and Taxonomies. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

Blogs are the ideal platform for people to express their opinions, opinions and opinions online. Many newbies are eager to build their own website but are hesitant to worry about technical barriers or cost issues. However, as the platform continues to evolve to meet the capabilities and needs of beginners, it is now starting to become easier than ever. This article will guide you step by step how to build a WordPress blog, from theme selection to using plugins to improve security and performance, helping you create your own website easily. Choose a blog topic and direction Before purchasing a domain name or registering a host, it is best to identify the topics you plan to cover. Personal websites can revolve around travel, cooking, product reviews, music or any hobby that sparks your interests. Focusing on areas you are truly interested in can encourage continuous writing

Recently, we showed you how to create a personalized experience for users by allowing users to save their favorite posts in a personalized library. You can take personalized results to another level by using their names in some places (i.e., welcome screens). Fortunately, WordPress makes it very easy to get information about logged in users. In this article, we will show you how to retrieve information related to the currently logged in user. We will use the get_currentuserinfo(); function. This can be used anywhere in the theme (header, footer, sidebar, page template, etc.). In order for it to work, the user must be logged in. So we need to use

Do you want to know how to display child categories on the parent category archive page? When you customize a classification archive page, you may need to do this to make it more useful to your visitors. In this article, we will show you how to easily display child categories on the parent category archive page. Why do subcategories appear on parent category archive page? By displaying all child categories on the parent category archive page, you can make them less generic and more useful to visitors. For example, if you run a WordPress blog about books and have a taxonomy called "Theme", you can add sub-taxonomy such as "novel", "non-fiction" so that your readers can

In the past, we have shared how to use the PostExpirator plugin to expire posts in WordPress. Well, when creating the activity list website, we found this plugin to be very useful. We can easily delete expired activity lists. Secondly, thanks to this plugin, it is also very easy to sort posts by post expiration date. In this article, we will show you how to sort posts by post expiration date in WordPress. Updated code to reflect changes in the plugin to change the custom field name. Thanks Tajim for letting us know in the comments. In our specific project, we use events as custom post types. Now

Are you looking for ways to automate your WordPress website and social media accounts? With automation, you will be able to automatically share your WordPress blog posts or updates on Facebook, Twitter, LinkedIn, Instagram and more. In this article, we will show you how to easily automate WordPress and social media using IFTTT, Zapier, and Uncanny Automator. Why Automate WordPress and Social Media? Automate your WordPre

One of our users asked other websites how to display the number of queries and page loading time in the footer. You often see this in the footer of your website, and it may display something like: "64 queries in 1.248 seconds". In this article, we will show you how to display the number of queries and page loading time in WordPress. Just paste the following code anywhere you like in the theme file (e.g. footer.php). queriesin

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.
