{"id":7044,"date":"2020-02-03T13:13:15","date_gmt":"2020-02-03T13:13:15","guid":{"rendered":"https:\/\/www.hostinger.com\/tutorials\/?p=7044"},"modified":"2024-03-26T12:44:44","modified_gmt":"2024-03-26T12:44:44","slug":"wordpress-custom-post-types","status":"publish","type":"post","link":"\/my\/tutorials\/wordpress-custom-post-types","title":{"rendered":"How to Create a WordPress Custom Post Type: What It Is and Its Customization"},"content":{"rendered":"<p>WordPress has multiple post types to help you organize your website&rsquo;s content. You can easily create a chronological blog or display static pages with them.<\/p><p>However, the built-in post types may not be sufficient for large websites with diverse content. In that case, you need to create your own post type.<\/p><p>This article will explain how to create a custom post type in WordPress. You will also learn how to customize it to expand its functionality.<\/p><p class=\"has-text-align-center\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Mega-WordPress-Cheat-EN.pdf\" target=\"_blank\" rel=\"noopener\">Download all in one WordPress cheat sheet<\/a><\/p><p>\n\n\n\n\n\n<div class=\"protip\">\n                    <h2 class=\"featured-snippet title\">What Is WordPress Custom Post Type?<\/h2>\n                    <p> WordPress custom post type is an additional content category users create to organize their website content. It is useful if your WordPress website has diverse content that doesn&rsquo;t belong to WordPress&rsquo;s default post types.<\/p>\n                <\/div>\n\n\n\n<\/p><p>While you can assign categories to organize your content, your posts will appear in one list when grouped into the same post type. This makes tracking your posts more difficult.<\/p><p>By default, WordPress has seven post types:<\/p><ol class=\"wp-block-list\">\n<li><strong>Posts<\/strong> &ndash; category for regular posts. Typical content of this type includes a blog post.<\/li>\n\n\n\n<li><strong>Pages<\/strong> &ndash; displays static content that is not listed by date.<\/li>\n\n\n\n<li><strong>Attachments<\/strong> &ndash; all the website media, including images, videos, or PDF files.<\/li>\n\n\n\n<li><strong>Revisions <\/strong>&ndash; saved versions of all post types.<\/li>\n\n\n\n<li><strong>Navigation Menus <\/strong>&ndash; list of links your website visitors can use to find specific information.<\/li>\n\n\n\n<li><strong>Custom CSS <\/strong>&ndash; type that you can modify to customize your WordPress theme.<\/li>\n\n\n\n<li><strong>Changesets <\/strong>&ndash; the track of changes made in the customizer.<\/li>\n<\/ol><p>WordPress custom post types allow you to choose a particular section for your content. You can quickly group content without choosing categories and easily track posts in different sections.<\/p><p>Custom post types in WordPress also offer several customization options. This allows you to expand their functionality beyond the default posts.<\/p><p>For instance, you can move the custom post type menu, change the edit screen&rsquo;s appearance, and enable features like featured images or excerpts.<\/p><h2 class=\"wp-block-heading\" id=\"h-elements-of-a-wordpress-custom-post-type\">Elements of a WordPress Custom Post Type<\/h2><p>Before creating custom post types, you must understand the terms<strong> array <\/strong>and <strong>element<\/strong>. They are essential parts of your custom post type&rsquo;s code.<\/p><p>An <strong>array <\/strong>is a data structure that stores key and value pairs known as <strong>elements<\/strong>. In this case, it reserves the properties of the custom post type.<\/p><p>To create a custom WordPress post type, there are three arrays containing different elements &ndash; <strong>$args<\/strong>, <strong>$labels<\/strong>, and <strong>$supports<\/strong>. The associative array <strong>$args<\/strong> is short for arguments, which include various key-value pairs, including:<\/p><ul class=\"wp-block-list\">\n<li><strong>description<\/strong> &ndash; a short and descriptive summary of the post type. You can only display it in the post type&rsquo;s template.<\/li>\n\n\n\n<li><strong>public<\/strong> &ndash; changes WordPress custom post types&rsquo; visibility to authors and visitors. Choose <strong>TRUE<\/strong> to make it appear in the WordPress Dashboard and visitors&rsquo; custom query.<\/li>\n\n\n\n<li><strong>menu_position<\/strong> &ndash; defines the new post type&rsquo;s location in the WordPress admin menu. Refer to the <a href=\"https:\/\/developer.wordpress.org\/reference\/\" target=\"_blank\" rel=\"noopener\">WordPress codex page<\/a> for its value and positions.<\/li>\n\n\n\n<li><strong>has_archive<\/strong> &ndash; defines the custom post type archives, URL structure, and slug name based on parameter 1 of<strong> register_post_types()<\/strong>.<\/li>\n\n\n\n<li><strong>show_in_admin_bar <\/strong>&ndash; alters the post type&rsquo;s visibility in the upper admin bar under the <strong>+New<\/strong> section.<\/li>\n\n\n\n<li><strong>show_in_nav_menus<\/strong> &ndash; changes whether you can add posts of this type to navigation menus created via the <strong>Appearance<\/strong> &rarr; <strong>Menus<\/strong>.<\/li>\n\n\n\n<li><strong>query_var <\/strong>&ndash; sets whether site visitors can enter a post&rsquo;s type and title as a query in the URL to view it.<\/li>\n<\/ul><p>In addition to the elements, <strong>$args<\/strong> holds the <strong>$labels <\/strong>and <strong>$supports<\/strong> array containing different key-value pairs. We recommend creating a separate <strong>$labels<\/strong> variable to keep all the key and value pairs to make the code cleaner and easier to understand<\/p><p>The <strong>$label<\/strong> array defines the custom post type&rsquo;s text and holds the following keys:<\/p><ul class=\"wp-block-list\">\n<li><strong>name<\/strong> &ndash; the general plural name for the post type<\/li>\n\n\n\n<li><strong>singular_name<\/strong> &ndash; the name for a single post of the custom type<\/li>\n\n\n\n<li><strong>add_new<\/strong> &ndash; replaces the &lsquo;Add New&rsquo; text<\/li>\n\n\n\n<li><strong>add_new_item<\/strong> &ndash; changes the &lsquo;Add New Post&rsquo; text<\/li>\n\n\n\n<li><strong>edit_item<\/strong> &ndash; adjusts the &lsquo;Edit Post&rsquo; text<\/li>\n\n\n\n<li><strong>featured_image <\/strong>&ndash; modifies &lsquo;Featured Image&rsquo; in the post editor<\/li>\n\n\n\n<li><strong>set_featured_image<\/strong> &ndash; replaces &lsquo;Set Featured Image&rsquo;<\/li>\n\n\n\n<li><strong>menu_name <\/strong>&ndash; alters the text of the top-level link. The default text for the link is the name key<\/li>\n<\/ul><p>Meanwhile, the <strong>$supports <\/strong>array contains elements that define the custom post type&rsquo;s features. The <strong>TRUE<\/strong> value enables the following features:<\/p><ul class=\"wp-block-list\">\n<li>title<\/li>\n\n\n\n<li>editor<\/li>\n\n\n\n<li>author<\/li>\n\n\n\n<li>thumbnail<\/li>\n\n\n\n<li>excerpt<\/li>\n\n\n\n<li>trackbacks<\/li>\n\n\n\n<li>custom-fields<\/li>\n\n\n\n<li>comments<\/li>\n\n\n\n<li>revisions<\/li>\n\n\n\n<li>page-attributes<\/li>\n\n\n\n<li>post-formats<\/li>\n<\/ul><h2 class=\"wp-block-heading\" id=\"h-how-to-create-a-wordpress-custom-post-type\">How to Create a WordPress Custom Post Type<\/h2><p>There are three different ways to add custom post types in WordPress, and each has its pros and cons:<\/p><ul class=\"wp-block-list\">\n<li><strong>Using a normal plugin<\/strong> &ndash; most simple, but the custom post type will be deleted if you uninstall or deactivate the plugin.<\/li>\n\n\n\n<li><strong>Editing the theme&rsquo;s functions.php file<\/strong> &ndash; doesn&rsquo;t require an additional tool, but this method doesn&rsquo;t preserve the data after the theme update.<\/li>\n\n\n\n<li><strong>Creating a <\/strong><a href=\"https:\/\/developer.wordpress.org\/plugins\/\" target=\"_blank\" rel=\"noopener\"><strong>site-specific plugin <\/strong><\/a>&ndash; preserves the data and is highly customizable but requires technical skills.<\/li>\n<\/ul><p>In this article, we will use the third method. Before proceeding, we recommend you read our tutorial about <a href=\"\/my\/tutorials\/how-to-create-wordpress-plugin\">how to create a WordPress plugin<\/a> to learn more about the custom code.<\/p><p>To create custom post types, use<strong> <\/strong>the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/register_post_type\/\" target=\"_blank\" rel=\"noopener\">register_post_type()<\/a> function. This takes two parameters &ndash; the <strong>custom post type name<\/strong> and the <strong>$args <\/strong>array.<\/p><p>The plugin&rsquo;s code should also contain custom functions that call <strong>register_post_type()<\/strong>. Remember to hook these functions to the <strong>init <\/strong>action hook to allow the custom post type to register properly.<\/p><p>To avoid conflict with other plugins, your custom function should have a prefix like <strong>ht_<\/strong>.<\/p><p>Here is a code example for a <strong>Custom Article <\/strong>post type plugin:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\/*\nPlugin Name: Custom Post Types\nDescription: Add post types for custom articles\nAuthor: Hostinger Dev\n*\/\n\/\/ Hook ht_custom_post_custom_article() to the init action hook\nadd_action( 'init', 'ht_custom_post_custom_article' );\n\/\/ The custom function to register a custom article post type\nfunction ht_custom_post_custom_article() {\n    \/\/ Set the labels. This variable is used in the $args array\n    $labels = array(\n        'name'               =&gt; __( 'Custom Articles' ),\n        'singular_name'      =&gt; __( 'Custom Article' ),\n        'add_new'            =&gt; __( 'Add New Custom Article' ),\n        'add_new_item'       =&gt; __( 'Add New Custom Article' ),\n        'edit_item'          =&gt; __( 'Edit Custom Article' ),\n        'new_item'           =&gt; __( 'New Custom Article' ),\n        'all_items'          =&gt; __( 'All Custom Articles' ),\n        'view_item'          =&gt; __( 'View Custom Article' ),\n        'search_items'       =&gt; __( 'Search Custom Article' ),\n        'featured_image'     =&gt; 'Poster',\n        'set_featured_image' =&gt; 'Add Poster'\n    );\n\/\/ The arguments for our post type, to be entered as parameter 2 of register_post_type()\n    $args = array(\n        'labels'            =&gt; $labels,\n        'description'       =&gt; 'Holds our custom article post specific data',\n        'public'            =&gt; true,\n        'menu_position'     =&gt; 5,\n        'supports'          =&gt; array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ),\n        'has_archive'       =&gt; true,\n        'show_in_admin_bar' =&gt; true,\n        'show_in_nav_menus' =&gt; true,\n        'query_var'         =&gt; true,\n    );\n    \/\/ Call the actual WordPress function\n    \/\/ Parameter 1 is a name for the post type\n    \/\/ Parameter 2 is the $args array\n    register_post_type('article', $args);\n}<\/pre><p>Note that the code may vary depending on the custom post type. Once you finish writing the code, follow these steps to turn it into a plugin:<\/p><ol class=\"wp-block-list\">\n<li>Copy and paste the code into a text editor. For example, we will use Windows <strong>Notepad<\/strong>.<\/li>\n\n\n\n<li>Save the file. Name the file <strong>custom-post-type.php <\/strong>and select <strong>All Files <\/strong>from the <strong>Save as type <\/strong>dropdown menu.<\/li>\n\n\n\n<li>Click <strong>Save<\/strong>.<\/li>\n<\/ol><figure class=\"wp-block-image aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/The-custom-plugin-code-saved-as-a-php-file-with-Notepad.png\"><img decoding=\"async\" width=\"1024\" height=\"541\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/The-custom-plugin-code-saved-as-a-php-file-with-Notepad-1024x541.png\" alt=\"The custom plugin code saved as a php file with Notepad\n\" class=\"wp-image-73110\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-custom-plugin-code-saved-as-a-php-file-with-Notepad-1024x541.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-custom-plugin-code-saved-as-a-php-file-with-Notepad-300x158.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-custom-plugin-code-saved-as-a-php-file-with-Notepad-150x79.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-custom-plugin-code-saved-as-a-php-file-with-Notepad-768x406.png 768w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-custom-plugin-code-saved-as-a-php-file-with-Notepad.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><ol class=\"wp-block-list\" start=\"4\">\n<li>Open your FTP client or hosting account&rsquo;s file manager. For Hostinger users, go to your <strong>hPanel <\/strong>&rarr; <strong>Hosting<\/strong> &rarr; <strong>Manage<\/strong> <strong>&rarr;<\/strong> <strong>Files &rarr;<\/strong> <strong>File Manager<\/strong>.<\/li>\n\n\n\n<li>Inside the<strong> File Manager<\/strong>, navigate to <strong>public_html <\/strong>&rarr;<strong> wp-content <\/strong>&rarr;<strong> plugins<\/strong>.<\/li>\n\n\n\n<li>Create a new folder and name it <strong>custom-post-type<\/strong>.<\/li>\n\n\n\n<li>Paste the<strong> <\/strong>file into the folder.<\/li>\n<\/ol><figure class=\"wp-block-image aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/The-custom-plugin-php-file-inside-the-website_s-plugin-folder.png\"><img decoding=\"async\" width=\"1024\" height=\"550\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/The-custom-plugin-php-file-inside-the-website_s-plugin-folder-1024x550.png\" alt=\"The custom plugin php file inside the website's plugin folder\n\" class=\"wp-image-73111\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-custom-plugin-php-file-inside-the-website_s-plugin-folder.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-custom-plugin-php-file-inside-the-website_s-plugin-folder-300x161.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-custom-plugin-php-file-inside-the-website_s-plugin-folder-150x81.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-custom-plugin-php-file-inside-the-website_s-plugin-folder-768x412.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><ol class=\"wp-block-list\" start=\"8\">\n<li>Log in to your WordPress dashboard. Go to <strong>Plugins <\/strong>&rarr; <strong>Installed Plugins<\/strong>.<\/li>\n\n\n\n<li>Find the <strong>Custom Post Type <\/strong>plugin and click <strong>Activate<\/strong>.<\/li>\n<\/ol><figure class=\"wp-block-image aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/Custom-post-type-plugin-activation-setting.png\"><img decoding=\"async\" width=\"1024\" height=\"421\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/Custom-post-type-plugin-activation-setting-1024x421.png\" alt=\"Custom Post Type plugin activation setting\n\" class=\"wp-image-73112\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Custom-post-type-plugin-activation-setting-1024x421.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Custom-post-type-plugin-activation-setting-300x123.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Custom-post-type-plugin-activation-setting-150x62.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Custom-post-type-plugin-activation-setting-768x316.png 768w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Custom-post-type-plugin-activation-setting.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><ol class=\"wp-block-list\" start=\"10\">\n<li>Refresh the page. You should see the <strong>Custom Article <\/strong>post type on your WordPress admin menu.<\/li>\n<\/ol><figure class=\"wp-block-image aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/A-custom-post-type-in-the-WordPress-sidebar.png\"><img decoding=\"async\" width=\"1024\" height=\"292\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/A-custom-post-type-in-the-WordPress-sidebar-1024x292.png\" alt=\"A custom post type in the WordPress sidebar\n\" class=\"wp-image-73113\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/A-custom-post-type-in-the-WordPress-sidebar-1024x292.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/A-custom-post-type-in-the-WordPress-sidebar-300x86.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/A-custom-post-type-in-the-WordPress-sidebar-150x43.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/A-custom-post-type-in-the-WordPress-sidebar-768x219.png 768w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/A-custom-post-type-in-the-WordPress-sidebar.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><p>You may create an unlimited number of custom post types. However, too many custom post types can slow the <a href=\"\/my\/tutorials\/wordpress-rest-api\">REST API<\/a> discovery and complicate the admin bar.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-customize-new-post-type\">How to Customize New Post Type<\/h2><p>You may need to customize your new post type to differentiate it from other content. Before customizing, you should create template files in your active theme&rsquo;s directory.<\/p><p>You need two template files for customization &ndash;<strong> single-{post_type}.php <\/strong>and <strong>archive-{post_type}.php<\/strong>. These are templates for an individual post entry and the custom type&rsquo;s archive page, respectively.<\/p><p>WordPress will use the default <strong>single.php <\/strong>and <strong>archive.php<\/strong> if you don&rsquo;t create these templates. Since all posts and archives refer to those two files, customizing them will change the entire site.<\/p><p>The best way to create template files is to duplicate your theme&rsquo;s existing <strong>single.php <\/strong>and <strong>archive.php <\/strong>files. This method keeps your site&rsquo;s theme structure and sets up the required template tag, simplifying the customization process.<\/p><p>Here are the steps to do so:<\/p><ol class=\"wp-block-list\">\n<li>Access your FTP client or hosting account&rsquo;s file manager.<\/li>\n\n\n\n<li>Locate your currently active theme folder. In our case, it is in <strong>\/public_html\/wp-content\/themes\/twentytwentyone<\/strong>.<\/li>\n\n\n\n<li>Copy the <strong>single.php<\/strong> and <strong>archive.php<\/strong> files and paste them outside that folder.<\/li>\n<\/ol><figure class=\"wp-block-image aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/Single.php-and-Archive.php-outside-the-active-theme-folder.png\"><img decoding=\"async\" width=\"1024\" height=\"551\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/Single.php-and-Archive.php-outside-the-active-theme-folder-1024x551.png\" alt=\"Single.php and Archive.php outside the active theme folder\n\" class=\"wp-image-73114\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Single.php-and-Archive.php-outside-the-active-theme-folder.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Single.php-and-Archive.php-outside-the-active-theme-folder-300x161.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Single.php-and-Archive.php-outside-the-active-theme-folder-150x81.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Single.php-and-Archive.php-outside-the-active-theme-folder-768x413.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><ol class=\"wp-block-list\" start=\"4\">\n<li>Rename them based on parameter 1 of your <strong>register_post_type()<\/strong>. Since our parameter 1 is <strong>article<\/strong>, the names are <strong>single-article.php<\/strong> and <strong>archive-article.php<\/strong>.<\/li>\n\n\n\n<li>Move the files back to the <strong>twentytwentyone<\/strong> folder.<\/li>\n<\/ol><figure class=\"wp-block-image aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/Template-files-inside-the-currently-active-theme-folder.png\"><img decoding=\"async\" width=\"1024\" height=\"552\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/Template-files-inside-the-currently-active-theme-folder-1024x552.png\" alt=\"Template files inside the currently active theme folder\n\" class=\"wp-image-73115\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Template-files-inside-the-currently-active-theme-folder.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Template-files-inside-the-currently-active-theme-folder-300x162.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Template-files-inside-the-currently-active-theme-folder-150x81.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Template-files-inside-the-currently-active-theme-folder-768x414.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><ol class=\"wp-block-list\" start=\"6\">\n<li>Repeat the second and third steps for other custom post type templates.<\/li>\n<\/ol><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> Themes with the same structure for posts and pages won&rsquo;t have <strong>single.php<\/strong> and archive.php files. Such themes only use the <strong>singular.php file<\/strong>.<\/p><\/div>\n\n\n\n<\/p><p>To customize the template files, download and edit them using a text editor. If you use Hostinger&rsquo;s <a href=\"\/my\/web-hosting\">web hosting service<\/a>, edit them directly in the file manager.<\/p><p>Once you save the files, the changes should apply only to the custom type&rsquo;s archive page and individual entry.<\/p><h3 class=\"wp-block-heading\" id=\"h-adding-a-meta-box-to-a-custom-post-type\">Adding a Meta Box to a Custom Post Type<\/h3><p>The meta box is an editing screen window allowing users to add post metadata. It is useful for adding or modifying posts&rsquo; information, such as author details, publishing controls, and custom slug.<\/p><p>While WordPress comes with default meta boxes, you may need a specific one for your custom post type. For example, the <strong>Movies <\/strong>custom post type may require the <strong>Genre <\/strong>meta box.<\/p><p>Using a meta box helps you add metadata to your content more quickly. Instead of entering the value individually, you can click data from the meta box to input it.<\/p><p>To create a custom meta box, edit your theme&rsquo;s <strong>functions.php <\/strong>or site-specific plugin&rsquo;s code. In this tutorial, we will add the new meta box&rsquo;s custom code to the plugin&rsquo;s file.<\/p><p>Since the code varies depending on the meta box content and placement, refer to WordPress codex for more information. For this tutorial, we will explain how to build a custom <strong>Author <\/strong>meta box:<\/p><ol class=\"wp-block-list\">\n<li>Locate the <strong>custom-post-type.php<\/strong> file in your hosting file manager or FTP client.<\/li>\n\n\n\n<li>Double-click the file to open the editor screen. Alternatively, download and open the file in a text or code editor.<\/li>\n\n\n\n<li>Create an empty custom meta box by entering the following code:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Add meta box \nadd_action(\"add_meta_boxes\", \"add_author_meta_box\");\n\nfunction add_author_meta_box()\n{\n    add_meta_box(\n    \"author_meta_box\", \/\/ Meta box ID\n    \"Author Details\", \/\/ Meta box title\n    \"author_meta_box_callback\", \/\/ Meta box callback function\n    \"article\", \/\/ The custom post type parameter 1\n    \"side\", \/\/ Meta box location in the edit screen\n    \"high\" \/\/ Meta box priority\n); \n}\nfunction author_meta_box_callback()\n{ \n\twp_nonce_field(&lsquo;author-nonce&rsquo;, &lsquo;meta-box-nonce&rsquo;);\nglobal $post;\nplaceholder\n}<\/pre><ol class=\"wp-block-list\" start=\"4\">\n<li>Add the meta box&rsquo;s content by replacing the <strong>placeholder<\/strong> value. For this tutorial, we will add the author name and ID fields using the following code.<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Add meta box content \n ?&gt;\n\t&lt;th&gt;&lt;label for=\"athor_name_field\"&gt;Author Name&lt;\/label&gt;&lt;th&gt;\n\t&lt;td&gt;&lt;input \ntype=\"text\"\nid=\"author_name\" \nclass=\"regular-text\" \nname=&ldquo;Author_Name&rdquo; \nvalue=&ldquo;&rdquo; \n\/&gt; \n    &lt;td&gt; \n    &lt;th&gt;&lt;label for=\"author_id_field\"&gt;Author ID&lt;\/label&gt;&lt;th&gt;\n\t&lt;td&gt;&lt;input \ntype=\"text\" \nid=\"author_id\" \nclass=\"regular-text\"\nname=&ldquo;Author_ID&rdquo;\nValue=&rdquo;&rdquo;\n\/&gt; \n    &lt;td&gt; \n\t&lt;?php  <\/pre><ol class=\"wp-block-list\" start=\"5\">\n<li>At the bottom of the code, add the following snippet to save the entered values:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Save meta box data\nadd_action ( 'save_post', 'author_save_postdata');\nfunction author_save_postdata( $post_id ) {\n\n\/\/ If this is an autosave, our form has not been submitted\nif ( defined( 'DOING_AUTOSAVE' ) &amp;&amp; DOING_AUTOSAVE ) \nreturn $post_id;\n\n\t\/\/ Retrieve post id\n\tIf (&lsquo;article&rsquo; !== get_post_type() ) {\n\t\treturn $post_id\n}\n \t\/\/ Check the user's permissions\n   \tif ( 'page' == $_POST['post_type'] ) {\n  \nif ( ! current_user_can( 'edit_page', $post_id ) )\nreturn $post_id;\n  \n} else {\n  \nif ( ! current_user_can( 'edit_post', $post_id ) )\nreturn $post_id;\n}\n  \n\/* OK, it is safe to save the data now. *\/\n  \n\/\/ Sanitize user input.\n\n$mydata = sanitize_text_field( $_POST['Author_Name'] );\n\n$mydata = sanitize_text_field( $_POST['Author_ID'] );\n\n  \n\/\/ Update the meta field in the database.\n\nupdate_post_meta( $post_id, 'Author_Name', $_POST['Author_Name'] ); \n\nupdate_post_meta( $post_id, 'Author_ID', $_POST['Author_ID'] ); \n }<\/pre><ol class=\"wp-block-list\" start=\"6\">\n<li>In the file manager&rsquo;s code editor, click the <strong>disk icon<\/strong> on the top right to save the change. If you edit the code locally, save and re-upload the updated file.<\/li>\n<\/ol><p>The new meta box should appear if you open your custom post type&rsquo;s edit screen.<\/p><figure class=\"wp-block-image aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/A-custom-author-details-meta-box-in-WordPress-post-edit-screen.png\"><img decoding=\"async\" width=\"1024\" height=\"432\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/A-custom-author-details-meta-box-in-WordPress-post-edit-screen-1024x432.png\" alt=\"A custom author details meta box in the WordPress post edit screen\n\" class=\"wp-image-73117\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/A-custom-author-details-meta-box-in-WordPress-post-edit-screen-1024x432.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/A-custom-author-details-meta-box-in-WordPress-post-edit-screen-300x127.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/A-custom-author-details-meta-box-in-WordPress-post-edit-screen-150x63.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/A-custom-author-details-meta-box-in-WordPress-post-edit-screen-768x324.png 768w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/A-custom-author-details-meta-box-in-WordPress-post-edit-screen.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><p>To ensure the meta box works properly, enter texts into the designated field and update the post. The entered value should appear in the custom fields.<\/p><p>If you need to become more familiar with PHP, use the <a href=\"https:\/\/wordpress.org\/plugins\/meta-box\/\" target=\"_blank\" rel=\"noopener\">Meta Box<\/a> plugin to automatically generate the code. Since it only generates the meta boxes&rsquo; content code, you must write the <strong>add_metabox () <\/strong>function yourself.<\/p><p>After you download and install the plugin, follow these steps:<\/p><ol class=\"wp-block-list\">\n<li>On the WordPress sidebar, go to <strong>Plugins <\/strong>&rarr; <strong>Installed Plugins<\/strong>.<\/li>\n\n\n\n<li>Find the <strong>Meta Box <\/strong>plugin and click <strong>About<\/strong> <strong>&rarr; Go to Online Generator<\/strong>.<\/li>\n<\/ol><figure class=\"wp-block-image aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/The-Meta-Box-plugin-in-WordPress_s-installed-plugins-menu.png\"><img decoding=\"async\" width=\"1024\" height=\"337\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/The-Meta-Box-plugin-in-WordPress_s-installed-plugins-menu-1024x337.png\" alt=\"The Meta Box plugin in WordPress's installed plugins menu\n\" class=\"wp-image-73118\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-Meta-Box-plugin-in-WordPress_s-installed-plugins-menu-1024x337.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-Meta-Box-plugin-in-WordPress_s-installed-plugins-menu-300x99.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-Meta-Box-plugin-in-WordPress_s-installed-plugins-menu-150x49.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-Meta-Box-plugin-in-WordPress_s-installed-plugins-menu-768x252.png 768w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-Meta-Box-plugin-in-WordPress_s-installed-plugins-menu.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><ol class=\"wp-block-list\" start=\"3\">\n<li>Create a custom meta box.<\/li>\n\n\n\n<li>Click <strong>Generate Code<\/strong> &rarr; <strong>Copy<\/strong>.<\/li>\n<\/ol><figure class=\"wp-block-image aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/Meta-Box-website_s-generate-custom-meta-box-code-button.png\"><img decoding=\"async\" width=\"1024\" height=\"487\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/Meta-Box-website_s-generate-custom-meta-box-code-button-1024x487.png\" alt=\"Meta Box website's generate custom meta box code button\n\" class=\"wp-image-73119\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Meta-Box-website_s-generate-custom-meta-box-code-button-1024x487.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Meta-Box-website_s-generate-custom-meta-box-code-button-300x143.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Meta-Box-website_s-generate-custom-meta-box-code-button-150x71.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Meta-Box-website_s-generate-custom-meta-box-code-button-768x365.png 768w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Meta-Box-website_s-generate-custom-meta-box-code-button.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><ol class=\"wp-block-list\" start=\"5\">\n<li>Paste the code to your <strong>functions.php <\/strong>or <strong>custom-post-type.php <\/strong>file.<\/li>\n<\/ol><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p> Use a plugin to <a href=\"\/my\/tutorials\/wordpress\/how-to-add-php-code-to-wordpress-post-or-page\">add PHP code in WordPress posts or pages<\/a> directly via the admin dashboard. You don&rsquo;t have to create template files or use a separate text editor, improving workflow and minimizing errors. <\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-display-custom-post-types-on-the-front-page\">How to Display Custom Post Types on the Front Page<\/h2><p>WordPress custom post types are not displayed on the front page by default. To enable them, write a new function that calls the set method of <a href=\"\/my\/tutorials\/wordpress-wp_query\">WordPress&rsquo; WP_Query<\/a> object.<\/p><p>Here&rsquo;s an example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Pin our custom function to the pre_get_posts action hook\nadd_action( 'pre_get_posts', 'add_article_to_frontpage' );\n\/\/ Alter the main query\nfunction add_article_to_frontpage( $query ) {\n if ( is_home() &amp;&amp; $query-&gt;is_main_query() ) {\n        $query-&gt;set( 'post_type', array( 'post', 'article' ) );\n    }\n    return $query;\n}<\/pre><p>In this function, <strong>$query-&gt;set()<\/strong> takes two parameters &ndash; the modified property and an array defining its value. In our case, the modified property is <strong>post_type<\/strong>, while the arrays&rsquo; values are <strong>post <\/strong>and <strong>article<\/strong>.<\/p><p>The array includes &lsquo;<strong>post<\/strong>&rsquo; to show the default post type on the front of your website. Removing it will display only the custom posts.<\/p><p>The second parameter is &lsquo;<strong>article<\/strong>&rsquo;, which displays regular and all custom article posts on the front page.<\/p><p>To enable the code, paste it to your theme&rsquo;s <strong>functions.php <\/strong>or custom plugin&rsquo;s file. We will show you how to paste it into the plugin:<\/p><ol class=\"wp-block-list\">\n<li>Access your WordPress site files and go to your custom plugin&rsquo;s folder. Ours is in the <strong>\/public_html\/wp-content\/plugins\/custom-post-type<\/strong>.<\/li>\n\n\n\n<li>Open the <strong>.php<\/strong> file. Then, copy and paste the code at the bottom.<\/li>\n<\/ol><figure class=\"wp-block-image aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/The-code-for-displaying-custom-post-type-in-the-plugin_s-file.png\"><img decoding=\"async\" width=\"1024\" height=\"551\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/The-code-for-displaying-custom-post-type-in-the-plugin_s-file-1024x551.png\" alt=\"The code for displaying custom post type in the plugin's file\n\" class=\"wp-image-73120\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-code-for-displaying-custom-post-type-in-the-plugin_s-file.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-code-for-displaying-custom-post-type-in-the-plugin_s-file-300x162.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-code-for-displaying-custom-post-type-in-the-plugin_s-file-150x81.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-code-for-displaying-custom-post-type-in-the-plugin_s-file-768x413.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><ol class=\"wp-block-list\" start=\"3\">\n<li>Click the <strong>disk<\/strong> <strong>icon<\/strong> on the top right to save your changes.<\/li>\n<\/ol><p>The default and custom post types should be available on the front of your WordPress website.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-add-and-display-wordpress-custom-field\">How to Add and Display WordPress Custom Field<\/h2><p>After creating a new custom post type, you may need to add a custom field to give your content more details.<\/p><p>For this task, you can use WordPress&rsquo;s built-in custom field or a plugin. Since every method has its pros and cons, we&rsquo;ll explain both to help you decide which suits you better.<\/p><h3 class=\"wp-block-heading\" id=\"h-using-wordpress-default-custom-field\">Using WordPress Default Custom Field<\/h3><p>WordPress provides us with a built-in tool to add a custom field. Since it is hidden by default, activate it by going to the <strong>Post Editor <\/strong>&rarr; <strong>Screen Option<\/strong>.<\/p><figure class=\"wp-block-image aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/The-Screen-Option-menu-in-WordPress-post-edit-screen.png\"><img decoding=\"async\" width=\"1024\" height=\"245\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/The-Screen-Option-menu-in-WordPress-post-edit-screen-1024x245.png\" alt=\"The Screen Option menu in WordPress post edit screen\n\" class=\"wp-image-73121\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-Screen-Option-menu-in-WordPress-post-edit-screen-1024x245.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-Screen-Option-menu-in-WordPress-post-edit-screen-300x72.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-Screen-Option-menu-in-WordPress-post-edit-screen-150x36.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-Screen-Option-menu-in-WordPress-post-edit-screen-768x184.png 768w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-Screen-Option-menu-in-WordPress-post-edit-screen.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><p>Once activated, you will see the custom field section at the bottom of the WordPress edit screen. Enter the name and value, and click the <strong>Add Custom Field<\/strong> button.<\/p><figure class=\"wp-block-image aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/WordPress_s-default-custom-field.png\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/WordPress_s-default-custom-field-1024x300.png\" alt=\"WordPress's default custom field\n\" class=\"wp-image-73122\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/WordPress_s-default-custom-field-1024x300.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/WordPress_s-default-custom-field-300x88.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/WordPress_s-default-custom-field-150x44.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/WordPress_s-default-custom-field-768x225.png 768w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/WordPress_s-default-custom-field.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><p>However, custom fields won&rsquo;t display on your page&rsquo;s front end by default. To enable them, insert <strong>the_meta()<\/strong> or <strong>echo get_post_meta()<\/strong> function into your active theme&rsquo;s file.<\/p><p>Use <strong>the_meta()<\/strong> to display all custom fields on a post. To display a specific custom field, use the <strong>echo get_post_meta( $post-&gt;ID, &lsquo;key&rsquo;, TRUE)<\/strong>. Replace the <strong>key<\/strong> value with your custom field name.<\/p><p>Since there are two post types, you can edit <strong>single.php<\/strong> or <strong>single-article.php<\/strong>. It depends on where you want to display the custom field.<\/p><p>Suppose you want to add an <strong>Author<\/strong> custom field for the custom article-type posts. In that case, enter one of the function codes in the <strong>single-article.php <\/strong>file. Here are the steps:<\/p><ol class=\"wp-block-list\">\n<li>From your WordPress Dashboard, head to <strong>Appearance <\/strong>&rarr; <strong>Theme Editor<\/strong>.<\/li>\n<\/ol><figure class=\"wp-block-image aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/Theme-file-editor-menu-in-the-WordPress-sidebar.png\"><img decoding=\"async\" width=\"1024\" height=\"424\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/Theme-file-editor-menu-in-the-WordPress-sidebar-1024x424.png\" alt=\"Theme file editor menu in the WordPress sidebar\n\" class=\"wp-image-73123\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Theme-file-editor-menu-in-the-WordPress-sidebar-1024x424.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Theme-file-editor-menu-in-the-WordPress-sidebar-300x124.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Theme-file-editor-menu-in-the-WordPress-sidebar-150x62.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Theme-file-editor-menu-in-the-WordPress-sidebar-768x318.png 768w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Theme-file-editor-menu-in-the-WordPress-sidebar.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><ol class=\"wp-block-list\" start=\"2\">\n<li>From the <strong>Theme FIles <\/strong>menu on the right side of your screen, select <strong>singe-article.php<\/strong>.<\/li>\n<\/ol><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/single-article.php-in-the-WordPress-theme-file-editor-screen.png\"><img decoding=\"async\" width=\"1024\" height=\"534\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/single-article.php-in-the-WordPress-theme-file-editor-screen-1024x534.png\" alt=\"single-article.php in the WordPress theme file editor screen\" class=\"wp-image-73124\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/single-article.php-in-the-WordPress-theme-file-editor-screen-1024x534.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/single-article.php-in-the-WordPress-theme-file-editor-screen-300x157.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/single-article.php-in-the-WordPress-theme-file-editor-screen-150x78.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/single-article.php-in-the-WordPress-theme-file-editor-screen-768x401.png 768w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/single-article.php-in-the-WordPress-theme-file-editor-screen.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><ol class=\"wp-block-list\" start=\"3\">\n<li>Locate the <a href=\"https:\/\/codex.wordpress.org\/the_loop\" target=\"_blank\" rel=\"noopener\">WordPress&rsquo; The Loop<\/a> and find this line:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">get_template_part( 'template-parts\/post\/content', get_post_format() );<\/pre><ol class=\"wp-block-list\" start=\"4\">\n<li>Paste one of the function codes under the line.<\/li>\n\n\n\n<li>Click <strong>Update File<\/strong>.<\/li>\n<\/ol><p>Your WordPress website will now show the <strong>Author<\/strong> custom field below the post.<\/p><figure class=\"wp-block-image aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/An-author-custom-field-displays-under-a-post.png\"><img decoding=\"async\" width=\"1024\" height=\"589\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/An-author-custom-field-displays-under-a-post-1024x589.png\" alt=\"An author custom field displayed under a post\n\" class=\"wp-image-73125\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/An-author-custom-field-displays-under-a-post-1024x589.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/An-author-custom-field-displays-under-a-post-300x173.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/An-author-custom-field-displays-under-a-post-150x86.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/An-author-custom-field-displays-under-a-post-768x442.png 768w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/An-author-custom-field-displays-under-a-post.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h3 class=\"wp-block-heading\" id=\"h-limiting-custom-fields-to-a-specific-post-type-via-a-plugin\">Limiting Custom Fields to a Specific Post Type via a Plugin<\/h3><p>Unfortunately, you can&rsquo;t use WordPress&rsquo;s built-in tool to add a custom field for a specific post type. This default tool automatically adds a new custom field for all post types.<\/p><p>Instead, use the <a href=\"https:\/\/wordpress.org\/plugins\/advanced-custom-fields\/\" target=\"_blank\" rel=\"noopener\">Advanced Custom Fields<\/a> plugin. It limits certain custom fields, making them selectable only on a chosen post type.<\/p><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> After this plugin is enabled, you will no longer see the default WordPress custom field on your post editor.<\/p><\/div>\n\n\n\n<\/p><p>After you activate the plugin, follow these steps to create and enable a custom field for a specific post type:<\/p><ol class=\"wp-block-list\">\n<li>Go to your WordPress sidebar, click <strong>Custom Fields <\/strong>&rarr; <strong>Add New<\/strong>.<\/li>\n<\/ol><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/The-add-new-custom-field-option-in-the-WordPress-sidebar.png\"><img decoding=\"async\" width=\"1024\" height=\"513\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/The-add-new-custom-field-option-in-the-WordPress-sidebar-1024x513.png\" alt=\"The add new custom field option in the WordPress sidebar\" class=\"wp-image-73126\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-add-new-custom-field-option-in-the-WordPress-sidebar-1024x513.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-add-new-custom-field-option-in-the-WordPress-sidebar-300x150.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-add-new-custom-field-option-in-the-WordPress-sidebar-150x75.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-add-new-custom-field-option-in-the-WordPress-sidebar-768x385.png 768w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/The-add-new-custom-field-option-in-the-WordPress-sidebar.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><ol class=\"wp-block-list\" start=\"2\">\n<li>Enter the <strong>New Field Group <\/strong>name.<\/li>\n\n\n\n<li>Fill in the following details for your new custom field:<\/li>\n<\/ol><ul class=\"wp-block-list\">\n<li><strong>Field Label<\/strong> &ndash; the name of your custom field that will be displayed on the edit page.<\/li>\n\n\n\n<li><strong>Field Name<\/strong> &ndash; the ID of your custom field that will be used to display on your theme.<\/li>\n\n\n\n<li><strong>Field Type<\/strong> &ndash; choose between text, checkbox, URL, etc., based on your needs.<\/li>\n<\/ul><ol class=\"wp-block-list\" start=\"4\">\n<li>Once you&rsquo;re done, hit <strong>Close Field<\/strong>.<\/li>\n\n\n\n<li>Scroll down to the <strong>Settings <\/strong>section and select the <strong>Location Rules <\/strong>tab.<\/li>\n\n\n\n<li>Fill the <strong>Show this field if <\/strong>columns<strong> <\/strong>to define the custom field&rsquo;s visibility condition. For example, to make it selectable on a <strong>Custom Article <\/strong>post type, choose <strong>Post Type <\/strong>&rarr; <strong>is equal to<\/strong> &rarr; <strong>Custom Article<\/strong>.<\/li>\n<\/ol><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/Advanced-Custom-Field-plugin_s-location-rules-settings.png\"><img decoding=\"async\" width=\"1024\" height=\"502\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/Advanced-Custom-Field-plugin_s-location-rules-settings-1024x502.png\" alt=\"Advanced Custom Field plugin's location rules settings\" class=\"wp-image-73127\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Advanced-Custom-Field-plugin_s-location-rules-settings.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Advanced-Custom-Field-plugin_s-location-rules-settings-300x147.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Advanced-Custom-Field-plugin_s-location-rules-settings-150x74.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2020\/02\/Advanced-Custom-Field-plugin_s-location-rules-settings-768x377.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><ol class=\"wp-block-list\" start=\"7\">\n<li>Click the <strong>Save Changes <\/strong>button.<\/li>\n<\/ol><p>Since you create this field via a plugin, you must use its functions to display the custom field. The steps to insert the function are similar to adding a default custom field, but enter <strong>the_post( &lsquo;key&rsquo; ); <\/strong>function instead.<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/my\/wordpress-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner-1024x300.png\" alt=\"\" class=\"wp-image-111781\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/06\/New-WP_in-text-banner-1024x300.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/06\/New-WP_in-text-banner-300x88.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/06\/New-WP_in-text-banner-150x44.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/06\/New-WP_in-text-banner-768x225.png 768w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/06\/New-WP_in-text-banner-1536x450.png 1536w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/06\/New-WP_in-text-banner.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>Custom post type in WordPress allows users to organize their website content into different categories. However, the default post types may not be enough for a large website with diverse content.<\/p><p>In that case, you can create a custom post type in WordPress by building site-specific plugins. Here&rsquo;s the recap of the steps:<\/p><ol class=\"wp-block-list\">\n<li>Write the plugin code in a text editor and save it as <strong>.php<\/strong>.<\/li>\n\n\n\n<li>Create a custom post type folder in your site&rsquo;s plugins directory.<\/li>\n\n\n\n<li>Paste the file into the new folder.<\/li>\n\n\n\n<li>Go to your <strong>WordPress admin dashboard<\/strong> &rarr; <strong>Plugins<\/strong> &rarr; <strong>Installed Plugins<\/strong><\/li>\n\n\n\n<li>Find the custom post type plugin and click <strong>Activate<\/strong>.<\/li>\n<\/ol><p>Your custom post type also has plenty of customization options. For instance, you can customize the editing screen, add meta boxes, edit the post type&rsquo;s visibility, and enable WordPress&rsquo;s built-in features.<\/p><p>This vast customizability lets you create a post type that specifically caters to your content. It helps you organize your WordPress website faster and more efficiently.<\/p><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Learn Other Expert WordPress Techniques<\/h4>\n                    <p><br>\n<a href=\"\/my\/tutorials\/wordpress-custom-css\">How to Add Custom CSS to WordPress<\/a><br>\n<a href=\"\/my\/tutorials\/wordpress-salts\">All About WordPress Salts<\/a><br>\n<a href=\"\/my\/tutorials\/how-to-create-custom-widget-in-wordpress%20\">How to Create WordPress Custom Widgets<\/a><br>\n<a href=\"\/my\/tutorials\/what-are-wordpress-hooks\/\">WordPress Hooks Guide<\/a><br>\n<a href=\"\/my\/tutorials\/wordpress-post-template\">How to Create a Custom Post Template in WordPress<\/a><br>\n<a href=\"\/my\/tutorials\/how-to-use-xampp-wordpress\/%20\">How to Use XAMPP to Set Up a Local WordPress Site<\/a><br>\n<a href=\"\/my\/tutorials\/wordpress\/how-to-add-php-code-to-wordpress-post-or-page\">How to Add PHP Code to WordPress<\/a><br>\n<a href=\"\/my\/tutorials\/how-to-duplicate-wordpress-page-or-post\">How to Duplicate Pages in WordPress<\/a><br>\n<a href=\"\/my\/tutorials\/wordpress-get-post-meta\">How to Display Custom Field Metadata Using WordPress get_post_meta Function<\/a><\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-wordpress-custom-post-type-faq\">WordPress Custom Post Type FAQ<\/h2><p>This section will answer three of the most common questions regarding WordPress&rsquo;s custom post type. However, if you have further questions, leave them in the comment action below.<\/p><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1671537075069\"><h3 class=\"schema-faq-question\">What&rsquo;s the Difference Between a Post and a Custom Post Type?<\/h3> <p class=\"schema-faq-answer\">In WordPress, a<strong> post <\/strong>may refer to a single piece of content. It may also refer to a <a href=\"https:\/\/wordpress.org\/support\/article\/post-types\/\" target=\"_blank\" rel=\"noopener\">post type<\/a>, similar to pages, attachments, revisions, and others.&nbsp;<br><br>Meanwhile, a<strong> custom post type <\/strong>is a content category that users make themselves.&nbsp;<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1671537092687\"><h3 class=\"schema-faq-question\">&#8203;&#8203;What Can You Change in the WordPress Custom Post Type?<\/h3> <p class=\"schema-faq-answer\">You can change its location in the admin area, searchability, and additional features like excerpts, <a href=\"\/my\/tutorials\/wordpress-post-formats\">post formats<\/a>, and trackbacks.&nbsp;<br><br>Moreover, WordPress also allows you to modify the edit post screen and add custom fields to include more information in your content. For instance, you can display the post&rsquo;s author or reading time.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1671537112837\"><h3 class=\"schema-faq-question\">Is It Possible to Get a Custom Post Type ID?<\/h3> <p class=\"schema-faq-answer\">Yes. To see a <a href=\"\/my\/tutorials\/wordpress-post-id\">WordPress post ID<\/a>, hover over the content&rsquo;s title and see the UR at the bottom left corner. It is the number between the &ldquo;<strong>post=<\/strong>&rdquo; and &ldquo;<strong>&amp;<\/strong>&rdquo; parameters.You can also get a custom post type unique ID by its title using the <strong>get_page_by_title () <\/strong>command. Alternatively, retrieve it using slug the <strong>get_page_by_path () <\/strong>command.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>WordPress has multiple post types to help you organize your website&rsquo;s content. You can easily create a chronological blog or display static pages with them. However, the built-in post types may not be sufficient for large websites with diverse content. In that case, you need to create your own post type. This article will explain [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/my\/tutorials\/wordpress-custom-post-types\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":72,"featured_media":75536,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to Create and Customize a WordPress Custom Post Type","rank_math_description":"Check out this article to learn how to use code and a plugin to create a WordPress custom post type, customize and display it on the website.","rank_math_focus_keyword":"wordpress custom post type","footnotes":""},"categories":[22638,22632],"tags":[],"class_list":["post-7044","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-advanced","category-wordpress"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/wordpress-custom-post-types","default":0},{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/como-criar-custom-post-types-wordpress","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/comment-creer-custom-post-type-wordpress","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/custom-post-types-wordpress","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/cara-membuat-custom-post-types","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/wordpress-custom-post-types","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/wordpress-custom-post-types","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/wordpress-custom-post-types","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/agregar-meta-descripcion-wordpress-5","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/agregar-meta-descripcion-wordpress-5","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/custom-post-types-wordpress","default":0},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/como-criar-custom-post-types-wordpress","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/wordpress-custom-post-types","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/wordpress-custom-post-types","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/wordpress-custom-post-types","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/wordpress-custom-post-types","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/7044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/users\/72"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/comments?post=7044"}],"version-history":[{"count":85,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/7044\/revisions"}],"predecessor-version":[{"id":100268,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/7044\/revisions\/100268"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/media\/75536"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/media?parent=7044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/categories?post=7044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/tags?post=7044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}