Whether or not to understand the post types, custom categorization methods is a simple way to determine whether a developer has begun WordPress development, will add custom post types and categorization methods, and based on this theme or plugin development is the way to advanced WordPress developers. This description is to use the register_post_type()
Function registration WordPress custom post types of the ultimate tutorial, read this tutorial, other similar do not need to see.
Here's what we did in a WordPress CRM systemThe type of articles registered in the center, feel free to feel.
What exactly is a custom article type
Yes, this is an important question, by understanding what a custom post type is, we see the custom post type, and it's up to your taste if you want to continue the relationship next. In fact, the custom post type is not a thing, he is a class, the name is "WP_Post_Type" (really straightforward), we use custom post type, we have to new out of this class object (if the girlfriend can also be so, tsk tsk ...), with the object, the next operation is centered around this object, usually we don't directly use the new WP_Post_Type
method to create the object, but instead use the register_post_type()
function.
First, let's look at the parameters supported by the register_post_type() function
following register_post_type()
function's parameters, adjusting them as needed, we can register just the right type of article for our needs.
__('This is a description for my post type.', 'wprs'),
// String
// Whether or not the post type is public for administrators or front-end users; the value of this parameter is the default for many of the following parameters
'public' => true, // bool (default is FALSE)
// bool (default is FALSE)
// Whether the article type can be queried on the frontend as part of parse_request()
'public_queryable' => true, // bool (default 'is FALSE)
// bool (default is the value of the 'public' parameter).
// Whether to hide the article type from front-end searches.
'exclude_from_search' => false, // bool (defaults to the value of the 'public' parameter).
// bool (defaults to the value of the 'public' inverse).
// Whether the article type can be selected in the navigation menu
'show_in_nav_menus' => false, // bool (defaults to 'public' inverse) // If the article type can be selected in the navigation menu.
// bool (defaults to the value of the 'public' parameter)
// Whether or not to generate a default admin UI in the admin UI, using the latter parameter, you can control the generated UI component, if we want to build our own admin UI
// Set this parameter to False
'show_ui' => true, // bool (defaults to 'show_ui')
// bool (defaults to the value of 'public')
// Whether or not to show it in the admin menu, the 'show_ui' parameter must be set to True for this parameter to be valid, our page can set this parameter to a top level menu
// (e.g., 'tools.php'), in which case the admin menu for the article type appears under the Tools menu
'show_in_menu' => true, // bool (defaults to 'tools.php')
// bool (defaults to the value of 'show_ui')
// Whether or not to show the post type in the admin toolbar; if set to true, WordPress will add a link to create a new post of the post type in the admin toolbar
'show_in_admin_bar' => true, // bool (defaults to 'ui'), // whether to show the post type in the admin toolbar.
// bool (defaults to the value of 'show_in_menu')
// The position of the article type in the admin menu, 'show_in_menu' must be set to true for this parameter to work
'menu_position' => null, // int (defaults to 25)
// int (defaults to 25 - appears after the 'comments' menu)
// The icon URI for the admin menu, or the class name of the Dashicon. See: https://developer.wordpress.org/resource/dashicons/
'menu_icon' => null, // string (defaults to 25 - appears after the 'comments' menu)
// string (defaults to post icon)
// Whether posts belonging to this post type can be exported via the WordPress Import/Export plugin or a plugin of type
'can_export' => true, // string (defaults to post icon)
// bool (TRUE by default)
// Whether the post is exposed to the Rest API
'show_in_rest', // boolean (defaults to TRUE)
// boolean (defaults to false)
// The base URI alias to access using the Rest API
'rest_base', // string, defaults to article type alias
// String, defaults to the article type alias
// Use a custom Rest API controller instead of the default WP_REST_Posts_Controller, custom controllers must inherit from WP_REST_Controller
'rest_controller_class'.
// string, defaults to WP_REST_Posts_Controller
// Whether to delete posts authored by users when they are deleted
'delete_with_user' => false, // bool (if the post type is deleted, it will be deleted if the post type is not deleted).
// bool (this parameter defaults to TRUE if the post type supports the 'author' feature)
// Whether the article type supports multiple levels of articles (parent/child/etc...)
'hierarchical' => false, // bool (defaults to FALSE)
// bool (defaults to FALSE)
// Whether or not to enable the index/archive/root page for this article type, if TRUE, the article type name will be used as an alias for the archive page, // if TRUE, the article type name will be used as an alias for the archive page.
// Of course, we can set a customized archive alias for our page
'has_archive' => 'example', // bool|string (bool|string (bool|string (bool|string))
// bool|string (default is FALSE)
// Set the query_var key for the article type, if it's set to TRUE, it will use the name of the article type, or set a custom string if needed
'query_var' => 'example', // bool|string (default FALSE) // Set the query_var key for the article type.
// bool|string (default is TRUE - article type name)
// The string used to build the edit, delete, and read permissions for the article type, either as a string or as an array, if the negative of the word is not in the form of an "s" we need to
// set an array, array( 'box', 'boxes' )
'capability_type' => 'example', // string|array (default), 'box', 'boxes' )
// string|array (defaults to 'post')
// Whether to let WordPress map permission metadata (edit_post, read_post, delete_post), if set to FALSE, we'll need to do this ourselves via the
// Filtering the 'map_meta_cap' hook to set post type permissions
'map_meta_cap' => true, // bool (defaults to FALSE).
// bool (defaults to FALSE)
// To set more precise post type permissions, WordPress uses the 'capability_type' parameter to build permissions by default, and in most cases we don't need permissions as complete as those for posts // or pages.
Most of the time, we don't need a full set of permissions for a post // or page. Here are a few permissions I use a lot: 'manage_examples', 'edit_examples', 'create_examples'.
// Each post type is unique, and we can adjust these permissions as needed.
'capabilities' => [
// meta caps (don't assign these to roles)
'edit_post' => 'edit_example', 'read_post' =>
'read_post' => 'read_example', 'delete_post' =>
// primitive/meta caps
'create_posts' => 'create_examples', // primitive/meta caps
// primitive caps used outside of map_meta_cap()
'edit_posts' => 'edit_examples', // primitive caps used outside of map_meta_cap()
'edit_others_posts' => 'manage_examples', 'publish_posts' =>
'read_private_posts' => 'read',
// primitive caps used inside of map_meta_cap()
'read' => 'read', // primitive caps used inside of map_meta_cap()
'delete_posts' => 'manage_examples', // primitive caps used inside of map_meta_cap()
'delete_published_posts' => 'manage_examples',
'delete_others_posts' => 'manage_examples', 'delete_private_posts' => 'manage_examples', 'delete_published_posts' => 'manage_examples'
'edit_published_posts' => 'edit_examples',
]
// Define the URL structure for the post type, we can set a specific parameter or a boolean value, if set to false the post type will not be supported
// URL Rewrite function
'rewrite' => [
// Alias for the article type
'slug' => 'example', // string (defaults to article type name)
// Whether to show $wp_rewrite->front article type alias in fixed links
'with_front' => false, // bool (defaults to TRUE)
// Whether to allow articles in the article type to pass shortcut tag to enable paging
'pages' => true, // bool (defaults to TRUE)
// Whether or not to create nice fixed links to feeds for subscription feeds.
'feeds' => true, // bool (defaults to the value of 'has_archive')
// Set endpoint masks for fixed links.
'ep_mask' => EP_PERMALINK, // const (defaults to EP_PERMALINK)
]
// A WordPress feature of post type support, many of the parameters are useful in the post editing interface. This helps other themes and plugins decide what features to let users use
// or what data to provide, we can either set an array for this parameter or set it to false to prevent any features from being added, after the post type is created we
//After a post type is created, we can add functionality with add_post_type_support() or remove functionality with remove_post_type_support(). The default functions are "title
// and "editor".
'supports' => [
'title', // Article title ($post->post_title).
'editor', // article content ($post->post_content).
'excerpt', // article summary ($post->post_excerpt).
'author', // article author ($post->post_author).
'thumbnail', // the featured image (the theme used by the current site must support 'post-thumbnails').
'comments', // show comments metadata box, if set, this post type will support comments
'trackbacks', // show the metadata box that allows link notifications in the edit screen
'custom-fields', // show metadata box for custom fields
'revisions', // display the revisions metadata box, if this parameter is set WordPress will store post versions in the database
'page-attributes', // Show 'page-attributes' metadata box, containing parent or page-order fields.
'post-formats', // Show the post formats metadata box and allow post formats for this post type
],
// tag is used to display the name of this post type in the admin interface or frontend, the tag parameter does not automatically rewrite the fields in the post update, error, etc. message, we need to filter it
// 'post_updated_messages' hook to customize these messages.
'labels' => [
'name' => __('Posts', 'wprs'),
'singular_name' => __('Posts', 'wprs'),
'menu_name' => __('Posts', 'wprs'),
'name_admin_bar' => __('Posts', 'wprs'),
'add_new' => __('Add New', 'wprs'),
'add_new_item' => __('Add New Post', 'wprs'),
'search_items' => __('Search Posts', 'wprs'),
'not_found_in_trash' => __('No posts found in trash', 'wprs'),
'all_items' => __('All Posts', 'wprs'),
'insert_into_item' => __('Insert into post', 'wprs'),
'uploaded_to_this_item' => __('Uploaded to this post', 'wprs'),
'list' => __('Posts list', 'wprs'),
// Tags used only in hierarchical post types
'parent_item' => __('Parent Post', 'wprs'), // tags used only in hierarchical post types
'parent_item_colon' => __('Parent Post:', 'wprs'), // tag used only in graded post types 'parent_item' => __('Parent Post', 'wprs'), // tag used only in graded post types
]
];
// Register the post type
register_post_type(
'example', // name of the article type, up to 20 characters, no uppercase or spaces supported
$args // Parameters for the article type
);
}
Creating Custom Post Types with Visual Plugins
For those of you who are used to UI manipulation, you can use some visual plugins to create custom types, such as the popular Custom Post Type UI, or the GenerateWP-supplied Post Type Generator Services.
Modify registered post types with the register_post_type_args filter
The above is an example of the use of register_post_type()
function to register a new post type, if we need a post type has been registered, WordPress provides us with a filter "register_post_type_args", using this filter, we can modify the parameters of the existing post type, so as to to modify the parameters of existing post types, so as to achieve the purpose of modifying the registered post types. For example, in the following example, we change the parameter of "order" post type to false, so as to realize the purpose of hiding the post type in the interface.
add_filter('register_post_type_args', function ($args, $post_type)
{
if ($post_type == 'order') {
$args[ 'public' ] = false;
}
return $args;
});
Modifying registered custom post types using helper functions
In addition to using the above filter methods to modify existing post types, WordPress also provides us with several functions to realize the common needs of post type modification.
add_post_type_support( 'post_type_name', array( 'title', 'editor' ) ); // add post type support
register_taxonomy_for_object_type( 'taxonomy_name', 'post_type_name' ); // register custom taxonomy
unregister_taxonomy_for_object_type( 'taxonomy_name', 'post_type_name' ); // remove custom taxonomy
Anti-registration/deletion of registered custom post types
There is a positive and a negative, a registration and a deletion, we can use the following function to remove an existing article type.
unregister_post_type('post_type_name')
Messing around with some things using custom custom post types
With a custom article type, it is a waste to leave it unused, we can also use custom article types with the help of the following functions to do some things. Limitations of space, these functions do not provide parameter interpretation, if necessary, please search to see the documentation, if you can not find the documentation, turn over the source code is also available.
Determine the nature of the type of article
is_post_type_hierarchical( $post_type ) // determine if the post type is hierarchical
post_type_exists( $post_type ) // determine if a post type exists
post_type_supports( $post_type, $feature ) // determine if the post type supports a feature
is_post_type_viewable( $post_type ) // determine if the post type is viewable on the frontend
Get the article type object and its properties
get_post_type( $post = null ) // Determine the post type of the given post
get_post_type_object( $post_type ) // Get the post type object
get_post_types( $args = array(), $output = 'names', $operator = 'and' ) // get all post types
get_post_type_labels( $post_type_object ) // get all post type labels
get_all_post_type_supports( $post_type ) // get all features supported by post type
get_post_types_by_support( $feature, $operator = 'and' ) // get all post types that support a feature
set_post_type( $post_id = 0, $post_type = 'post' ) // set the post type of the article
How do I know all this?
To put together the tutorial above, I've copied some stuff from the following three main places for those interested.
- Documentation:https://codex.wordpress.org/Function_Reference/register_post_type
- Source: /wp-includes/post.php
- Source: /wp-includes/class-wp-post-type.php
1 thoughts on “WordPress 自定义文章类型终极教程——注册到修改到删除”
Very detailed, thanks for sharing it with the big guy, I've written a little bit about this convenience on my own blog It quotes quite a bit 🤝
One thing I haven't figured out yet is about the ep_mask parameter
The Most Detailed Guide to Custom Post Types for WordPress Ever (1)
https://bestscreenshot.com/complete-guide-custom-post-types-part1/