How it works...

The default WordPress installation uses posts and pages as built-in post types for managing content. Apart from that, there are built-in post types such as media and menu, which are used for different purposes, even though they are stored in the wp_posts database table. In application development, we need advanced features beyond normal posts or pages. So, WordPress allows us to build and manage advanced content using Custom Post Types. This is a built-in feature that allows us to register any type of post type and handle the features for advanced application requirements while providing the fundamental features of a post, such as built-in storage, customizable list, tags, categories, custom fields, and so on.

We started this recipe by adding a code snippet for registering a new post type called book to handle data in a book store or a library. We used a callback function on the init action as the register_post_type function doesn't work before the init action. Inside the function, we configured a set of labels.

The code in this recipe was limited to a few labels for explanation purposes, while the code for this chapter provides more configurable labels. By default, these labels will display as Post in all locations. Even though it's not mandatory, it's recommended to configure these labels to a post-type-specific value to make the interfaces more user-friendly. You can find a complete list of labels at https://developer.wordpress.org/reference/functions/get_post_type_labels/.

A new post type in WordPress is registered using the register_post_type function. This function allows us to configure the settings for each post type by letting us pass them as an array of arguments. We have used some of the available arguments in this scenario. Let's take a look at them in detail@

  • label: This is the label for the post types to be displayed on the screen.
  • description: This is the description for the new post type.
  • labels: This is an array of values with book-specific labels to be displayed in various backend screens and messages.
  • supports: This setting defines what features should be enabled for this post type. In this case, we have specified title, editor, thumbnail, and custom-fields. This will enable the default title, content editor, featured image section, and the built-in custom fields section.
  • taxonomies: This setting defines the supported taxonomies for the post type. We can use default categories and tags or create post-type-specific taxonomies. In this case, we have used default categories and tags. 
  • public: This setting defines whether a post type is intended for use publicly either via the admin interface or by frontend users. We have set it to true to make the post type available to any user.
  • has_archive: This setting defines whether a post type has an archive page. This is set to false by default. Once it's set to true, the custom post type will be used as the slug for the archive page.
  • capability_type: The capabilities necessary for reading, inserting, updating, and deleting custom post type items. We have set it as a post in order to use normal post capabilities. We can also pass an array of custom capabilities based on our needs.
These are some of the most important arguments for the custom post type registration. You can view the complete list of arguments at https://codex.wordpress.org/Function_Reference/register_post_type.

Once the register_post_type function is called, you will get a new section in the left menu for each custom post type, similar to normal posts. The menu generally contains All Books, Add New, Tags, and Categories for each custom post type. The default Books menu item shows all the books created in the site. Initially, it will be an empty list. Then, we clicked the Add New button to create a new book. The book creation process is similar to the normal post creation process.

We added a title and content for the book and clicked the Publish button to create the book. The book will be saved in the wp_posts table with a post type of book instead of a normal post. Then, we viewed the book on the frontend of the site. We got a 404 error page instead of the actual book.

WordPress loads user requests based on available permalink structures. The default posts are loaded directly after the site name as http://www.yoursite.com/post_title. However, custom post types are loaded with custom URL structures. In this case, the URL structure will be http://www.yoursite.com/book/book_title. So, WordPress is not aware of additional book parameters in the URL and hence can't find a matching book. Therefore, the 404 page not found error page will be displayed. So, we have to update the WordPress permalink structure to support new rewriting rules that are created by custom post types.

Due to this, we clicked the Permalinks section in the Settings menu. This will automatically update the Permalink structure to support custom post types. Now, we can view the book on the frontend and the book details will be displayed instead of the 404 error page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.141.115