How to Create Custom Post Types in WordPress

Do you want to create a Custom Post Type in WordPress? It’s a great and useful way to organize your website if you’re dealing with multiple types of content at once.

In this tutorial, you will learn how to make a custom WordPress post type by building a site-specific plugin.

What are WordPress Custom Post Types?

WordPress provides a few default post types, such as Entry, Page, Revision Y attachments. On a small scale, these will work perfectly for your blog. However, they might not be enough when your content becomes more diverse.

That’s where the WordPress custom post type comes into play. In a nutshell, it is a type of additional post that you create based on your needs. With it, you can group your posts more specifically.

Creating a custom post type in WordPress is more efficient than assigning categories to your articles. That’s because, when you categorize items, they are placed in the same list, usually in the Tickets. The problem is that if you have several broad topics, it will be difficult to keep track of them.

On the other hand, it allows you to choose a more suitable section for your articles from the beginning. This should help you rank your content better and easier.

The WordPress custom post type has its own menu in the WordPress admin panel, which leads to a list of posts of that particular type.

For example, if you create a section of Personalized Items for your site, the publications of Entry either pages will not be included. That’s because they belong to their own types.

Not only that, you can also help readers narrow down their searches further by grouping the entries in your Personalized Items in different categories, such as Review, Tutorial, etc. Very convenient for your visitors, right?

What can you change in a WordPress Custom Post Type?

When creating a new post type, you have several customization options. You can choose where the menu appears in the admin area, if that type of post is included in search results, if it supports snippets, if comments are allowed, etc.

You can also change various text elements, such as add new entry by add new article wave Outstanding image by add poster.

Additionally, custom post types allow you to enable the custom fields feature in the post editor. Thanks to this, it is possible to give more details about your content.

Following the example of the post type Custom Articles, in addition to having the article as the main content, you have the option of adding custom fields for information such as Author, Reading timeetc.

See also  Hosting Agreement

However, any custom fields you create will be selectable on all post types. Therefore, a plugin is needed so that each field appears only for a specific type of post. We’ll explain how to do this later, so keep reading.

Elements of a WordPress Custom Post Type

To get started, let’s see how to write a new function that calls the function register_post_type(). It must have two parameters:

  • The name of the post type which must not be longer than 20 characters and must not contain spaces or capital letters. Our custom post type name is Article.
  • An associative array called $args containing information about the type of post in pairs ‘key’ => ‘value’.

IMPORTANT: Your function must be hooked to the action hook start. Otherwise, your custom post type will not register correctly.

Lastly, all custom functions should be prefixed to avoid conflicts with other plugin or theme functions. To tell them apart, we’ll use the Tutorials initials for this example: ht.

The initial code to add a new custom post type should look like this:

// The custom function MUST be hooked to the init action hook add_action( ‘init’, ‘ht_register_movie_post_type’ ); // A custom function that calls register_post_type function ht_register_movie_post_type() { // Sets various pieces of text, $labels is used within the array $args $labels = array( ‘name’ => _x( ‘Custom Posts’, ‘post type general name’ ), ‘singular_name’ => _x( ‘Custom Post’, ‘post type singular name’ ), … ); // Set various data about the post type $args = array( ‘labels’ => $labels, ‘description’ => ‘My custom post type’, ‘public’ => true, … ); // Register the type of post with all the information contained in the array $arguments register_post_type( ‘article’, $args ); }

Since so much $args What $labels are arrays, it is better to write the variable first $labels and then the variable $args. Once this is done, you can register the code.

Now, let’s discuss arrays $args Y $labels which we will use to customize the WordPress custom post type.

What is the $args array?

$args is short for arguments and this variable is used to hold arrays. Array, on the other hand, is a data structure that stores elements (key-value pairs) of an object (WordPress custom post type).

There are many arrays in $args, but we will list only the most used ones. However, keep in mind that they are all optional.

  • labels: An array that defines various pieces of text, such as the name of the menu and the name of the custom post type. We will talk more about this array later in the array section. $label.
  • description: a brief and descriptive summary of the type of post. This can be displayed in the post type template, but is not used anywhere else.
  • public: To change the visibility of the WordPress custom post type for authors and visitors. We are going to set the value to TRUE so that it appears in the WordPress dashboard.
  • menu_position: The position of the new link in the Control Panel navigation menu. The value 5 put it under Tickets Y 100 put it under Settings. You can see the full list of positions on the page.
  • supports– Selectively enable various post features such as featured images, excerpts, custom fields, etc. If set to FAKE instead of containing an array, it disables the editor for this type of post. This is useful if you want to block all posts of this type while keeping them visible.
  • has_archive: if set to TRUEdefines if the post type has an archive page, if the url follows your permalinks structure and if the slug is the name you entered in parameter 1 of register_post_types(). For example, http://www.mywebsite.com/articulo/ Show all posts from Personalized Items.
  • show_in_admin_bar: use the value TRUE will make this type of post appear in the top admin bar, in the menu +Add.
  • show_in_nav_menus: defines whether posts of this type can be added to navigation menus created via Appearance -> Menus. Here, we need to enter the value TRUE.
  • query_var: TRUE either FALSE sets whether a post can be viewed by typing the post type and post name as a query in the URL, i.e. ‘http://www.mywebsite.com/?article=example-article’.
See also  What is WebP? Beginner's Guide

What is the $labels array?

The first array of the variable $args it is $labels. The peculiarity is that it is made to contain other arrays. Therefore, it is crucial to create a variable called $labels to keep all key-value pairs in a separate section. This reduces the risk of writing bad code and makes the function cleaner.

Here are some of the most important keys for the array $labels:

  • yam: the general plural noun for the post type.
  • singular_name: The name for a single post of this type.
  • add_new: Replaces the ‘Add New’ text.
  • add_new_item: replaces ‘Add new entry’.
  • edit_item: Replaces ‘Edit Entry’.
  • featured_image: Replaces ‘Featured Image’ in the post editor.
  • set_featured_image: Replaces ‘Set Featured Image’.
  • menu_name: change the text of the top level link (the default text for the link is the name key).

What is the ‘supports’ array?

One of the keys of the array $args it is supports. This is a simple array where you write the list of editor features you want to enable for your custom post type. By default, only the title and the editor are enabled.

you can also write FALSE instead of an array. Doing this will disable all post editing features, including the title and content area. In this way, the posts cannot be edited, but are still visible.

Here is the list of editor features that you can enable in the array supports:

  • title
  • editor
  • author
  • thumbnail
  • exception
  • trackback
  • custom-fields
  • comments
  • reviews
  • page-attributes
  • post formats

Create the new custom post types through a plugin

To create a WordPress custom post type, you can create a plugin specific to your site, download a normal plugin, or edit your file. functions.php.

See also  How to put WordPress in maintenance mode

The third method is not recommended because your custom post type data could be overwritten after theme update. The second method is also unreliable as your data will be gone once you disable the tool.

So, let’s do the first option: build a site-specific plugin. Basically, it’s a custom tool that you can build yourself for specific tasks.

The good thing about this is that the data is preserved even if you decide to update your theme or disable the plugin. It is also highly customizable.

However, this method can be a bit technical. Therefore, to help you get started, we invite you to also read our tutorial on .

Now that we know what elements are needed for the function, we can build our plugin, write the custom functions, and hook them to the init action hook.

This is what a finished WordPress custom post type should look like:

ht_custom_post_article() to the init action hook add_action( ‘init ‘, ‘ht_custom_post_article’ ); // The custom function to register a custom post type function ht_custom_post_movie() { // Set the labels, this variable is used in the array $args $labels = array( ‘name’ => __( ‘Posts’ ), ​​’ singular_name’ => __( ‘Item’ ), ‘add_new’ => __( ‘Add New Item’ ), ‘add_new_item’ => __( ‘Add New Item’ ), ‘edit_item’ => __( ‘Edit Item’ ), ‘new_item’ => __( ‘New Item’ ), ‘all_items’ => __( ‘All Items’ ), ​​’view_item’ => __( ‘View Item’ ), ‘search_items’ => __( ‘ Find articles’ ), ​​…

Loading Facebook Comments ...
Loading Disqus Comments ...