popularity due to it moving beyond being a system limited to blogging, and its ability to create different types of websites. Perhaps the most prominent feature that has enhanced WordPress’s ability to achieve this is the Custom Post Types feature, which has enabled website owners to create different types of content on the site other than articles and pages .
In today’s article, we will explain in detail the concept of the custom post type and its importance, and learn how we can programmatically create custom post types within your WordPress theme, store them in the database, and deal with them through code.
Where are posts stored in WordPress?
Posts of all kinds are stored on the WordPress site within a table within the database called wp_posts , and the type of content is specified in the post_type column within this table. Through this column, we determine whether the stored value is an article (post), page (page), media, attachments, or Custom post etc..
Post type is very important for dealing with content in WordPress. If you are a WordPress template developer , you can create a special post type and create a new menu in the control panel dedicated to entering this post. You can also edit and delete it with ease on your WordPress site.
In the following paragraphs, we will learn how to create a custom post type with practical steps and how to display it on the front end of the site.
How are custom post types defined in WordPress?
Programmatically, the custom post type is a regular post, but the value of the post_type field in the database is different from the default post types in WordPress.
For example, if you are designing a WordPress template that displays information about books and you want to create your own post type to indicate the type of content of the book, you must first think about the fields that this book needs, such as (the name of the author of the book, the classification of this book, the cover image, the number of pages, the publishing house, etc. that..)
You can do this by creating a new file within the template folder and naming it with a name that expresses the type of custom post, such as books.php. It is preferable to save this file within a dedicated folder named (includes or inc) for ease of organization, then write a code in it whose task is to call the custom post registration function register_post_type. ()
The register_post_type function takes the following general form:
In the following example, we will explain how to use this function and pass parameters to it. Here, we will record a custom post that represents a book and add it to the free WordPress template Onepress as follows:
In this code, we defined a custom function (callback function) called create_book_cpt() and linked it to the init action hook . Within this function, as you can see, we called the post registration function register_post_type() after we knew the necessary arguments for this function to work.
We initially defined the parameter $labels, which is an array that specifies the various labels included in the custom post type. Then we defined the parameter $args, which represents the matrix of options for registering the custom publication type. It is also a matrix to which we pass a set of parameters or arguments. Here is the meaning of the most important of these parameters:
- labels Custom post field labels.
- description A brief explanation of the custom post type and its uses on the site.
- public is a boolean variable that sets a set of options. When we pass it the value true, this will set a set of other options related to viewing and querying the custom post to true as well.
- supports : This parameter provides custom post support for the default WordPress controls available in the editor. By default you can only display the ‘title’ and ‘editor’ fields for the default post, and add support for more elements such as the featured image
- ‘thumbnail’, ‘comments’, ‘revisions’, ‘author’, etc., you have to specify them manually through this parameter . (In the following link is a list of all the values that you can pass to this parameter)
- has_archive: If this parameter is set to true then a custom archive for this post will be created at the following link https://yourdomin/custom_post_type
- menu_position This option determines the position of the menu that displays the custom post type in the admin or dashboard control panel, as each tab or menu item in the control panel has a priority that determines its position as shown in the following image (a lower number means a higher priority).
After writing the file and saving your changes, you must import this file into the template functions file as follows:
Note: You can add the above books.php code directly to your theme’s functions.php file, but I personally prefer to define the custom post type in a separate file to keep the files organized and easy to maintain.
In the example above we created a simple custom post type but you can expand this type significantly, defining custom tags and categories for it or making tags and categories available for default posts for this custom post type.
Read also: Explaining and customizing taxonomies in WordPress
You can also control where the custom post type is displayed in the list, determine whether it is searchable or not, specify the permission of the user who can access it, customize the messages that WordPress displays for custom post types, and many other features.
After saving the book’s custom post file, the new custom book post type will appear in your site’s control panel (the back end of the site) in the form of a separate menu item. When we click on it, a submenu appears that includes 4 items as follows.
Now when you click on the Add New option in order to add a new book-type post, WordPress will take us to the following link
https://yourdomin/wp-admin/post-new.php?post_type=books
The traditional publications editor will open for us, but with field names designated for the book. For example, in our case, a tab with the name of a book appears instead of the tab (page or article), and a field with the name of the book cover appears instead of the featured image of the article or page, as follows.
These posts are saved similarly to the rest of the default posts in WordPress under the wp_posts table , but their post_type column value is books to distinguish them from the rest of the content types on the site.
View custom post types on your WordPress site
After we have defined the custom publication for books and added a group of books to the site’s database, we will of course need to display these books on the site. As we mentioned previously, if you passed the value trur to the has_archive parameter when creating the custom publication type books, WordPress will display an archive of these custom publications in A dedicated page with the following link: https://yourdomin/books/
Therefore, all you have to do is go to this link after writing your site’s domain instead of yourdomin in this link, followed by the name of the custom publication, and you will see a list or index of all the books you added as follows:
The template template file that is responsible by default for displaying this index according to the template file hierarchy is archive.php, if this is not present then WordPress will use the index.php file to display the book index.
If you want to format the book archive pages in a different way from the rest of the archive pages on the site, you can of course write a template file dedicated to this type called archive-books.php.
Read also: Customize the display of archive pages in a WordPress theme .
Alternatively, you can write the appropriate WP_Query to display these books in the way you need and in any place you need to display them within the site.
For example, to display only the book cover (featured image) and the book title, we can write the following query:
Of course, you must then write appropriate formats for the query results to appear in a way that suits your site design and visual identity.
Conclusion
With this, we have reached the end of our article today, in which we explained how to expand the features of the WordPress system, create additional features for the WordPress template, and make it support a custom post type according to the nature and purpose of the template.
Start by creating custom post types in your WordPress template that suit the nature of your site’s content. If you encounter any problem in dealing with this post, you can share it with us in the comments section below the article.
Leave a Reply