{"id":2533,"date":"2020-11-17T14:23:59","date_gmt":"2020-11-17T14:23:59","guid":{"rendered":"https:\/\/tschaki.com\/custom-post-type-in-wordpress\/"},"modified":"2021-11-20T15:12:03","modified_gmt":"2021-11-20T15:12:03","slug":"create-custom-post-type-in-wordpress","status":"publish","type":"post","link":"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/","title":{"rendered":"Create Custom Post Type in WordPress"},"content":{"rendered":"<h2 id=\"h-register-and-create-custom-post-type\">Register and create Custom Post Type<\/h2>\n\n\n\n<p>A Custom Post Type extends a WordPress website with another container to collect new posts in it. For example, you can use it to collect references, books or movies to output or collect them. This in a centralized way on the website. For example, we extend the website with a FAQ container with a Custom Post Type.<\/p>\n\n\n\n<p>To register the new Custom Post Type the following code is added to the functions.php of the active theme or plugin:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">\/*\n* Creating a function to create our CPT\n*\/\nfunction faq_custom_post_type() {\n \n    \/\/ Set UI labels for Custom Post Type\n    $labels = array(\n        'name' =&gt; _x( 'FAQ', 'FAQ', 'twentytwenty' ),\n        'singular_name' =&gt; _x( 'FAQ', 'FAQ', 'twentytwenty' ),\n        'menu_name' =&gt; __( 'FAQ', 'twentytwenty' ),\n        'all_items' =&gt; __( 'Alle FAQs', 'twentytwenty' ),\n        'view_item' =&gt; __( 'FAQ anzeigen', 'twentytwenty' ),\n        'add_new_item' =&gt; __( 'Neues FAQ', 'twentytwenty' ),\n        'add_new' =&gt; __( 'Neues FAQ', 'twentytwenty' ),\n        'edit_item' =&gt; __( 'FAQ bearbeiten', 'twentytwenty' ),\n        'update_item' =&gt; __( 'FAQ aktualisieren', 'twentytwenty' ),\n        'search_items' =&gt; __( 'Suche FAQ', 'twentytwenty' ),\n        'not_found' =&gt; __( 'Nicht gefunden', 'twentytwenty' ),\n        'not_found_in_trash' =&gt; __( 'Nicht gefunden im Papierkorb', 'twentytwenty' )\n    );\n     \n    \/\/ Set other options for Custom Post Type\n    $args = array(\n        'label' =&gt; __( 'faqs', 'twentytwenty' ),\n        'description' =&gt; __( 'FAQs', 'twentytwenty' ),\n        'labels' =&gt; $labels,\n        \/\/ Features this CPT supports in Post Editor\n        'supports' =&gt; array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),\n        \/\/ You can associate this CPT with a taxonomy or custom taxonomy. \n        'taxonomies' =&gt; array( 'category' ),\n        \/\/ A hierarchical CPT is like Pages and can have parent and child items. A non-hierarchical CPT is like Posts.\n        'hierarchical' =&gt; false,\n        'public' =&gt; true,\n        'show_ui' =&gt; true,\n        'show_in_menu' =&gt; true,\n        'show_in_nav_menus' =&gt; true,\n        'show_in_admin_bar' =&gt; true,\n        'menu_position' =&gt; 5,\n        'can_export' =&gt; true,\n        'has_archive' =&gt; true,\n        'exclude_from_search' =&gt; false,\n        'publicly_queryable' =&gt; true,\n        'capability_type' =&gt; 'post',\n        'show_in_rest' =&gt; true,\n    );\n     \n    \/\/ Registering your Custom Post Type\n    register_post_type( 'faq', $args );\n}\n \n\/* Hook into the 'init' action so that the function\n* Containing our post type registration is not \n* unnecessarily executed. \n*\/\nadd_action( 'init', 'faq_custom_post_type', 0 );<\/pre><\/div>\n\n\n\n<p>After inserting the code, all parameters in the code can be adjusted to change name, taxanomy or display icon in the backend. Now you can create new posts in the WordPress backend, which will be saved under the new <code>post_type<\/code>.<\/p>\n\n\n\n<h2 id=\"h-output-articles-of-the-custom-post-type\">Output articles of the Custom Post Type<\/h2>\n\n\n\n<p>To be able to output the created posts of the newly created Custom Post Type, a query is made to the database. Which only reads the posts with the appropriate post type.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$args = array( 'post_type' =&gt; 'faq', 'posts_per_page' =&gt; 10 );\n$the_query = new WP_Query( $args ); \n\/\/ Fetch 10 posts with post_type faq\nif ( $the_query-&gt;have_posts() ) : while ( $the_query-&gt;have_posts() ) : $the_query-&gt;the_post();\n\tthe_title();\n\tthe_content();\n}<\/pre><\/div>\n\n\n\n<p>In the above example, the title and content of the faq post type found in WordPress are output.<\/p>\n\n\n\n<p>In addition, WordPress now displays the new Custom Post Type in the menu in the backend.<\/p>\n\n\n\n<p>Source: <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/register_post_type\/\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress Codex register_post_type( )<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/tschaki.com\/downloads\" target=\"_blank\" rel=\"noreferrer noopener\">Downloads<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>One of the many advantages of a WordPress website is that it can be extended with content at will. One of the best functionalities is definitely the extension via so-called new Custom Post Type. <\/p>","protected":false},"author":1,"featured_media":2398,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[124,125],"tags":[126,127,128,129,130,131],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v16.6 (Yoast SEO v17.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Create Custom Post Type in WordPress &bull; tschaki<\/title>\n<meta name=\"description\" content=\"A Custom Post Type adds another vessel to a WordPress website to capture new posts or other attachments in it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create Custom Post Type in WordPress\" \/>\n<meta property=\"og:description\" content=\"A Custom Post Type adds another vessel to a WordPress website to capture new posts or other attachments in it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/\" \/>\n<meta property=\"og:site_name\" content=\"tschaki\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-17T14:23:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-20T15:12:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1580\" \/>\n\t<meta property=\"og:image:height\" content=\"500\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Trent Bojett\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/tschaki.com\/#organization\",\"name\":\"Tschaki.com\",\"url\":\"https:\/\/tschaki.com\/\",\"sameAs\":[],\"logo\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/tschaki.com\/#logo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/12\/tschaki-logo-1.png\",\"contentUrl\":\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/12\/tschaki-logo-1.png\",\"width\":1022,\"height\":273,\"caption\":\"Tschaki.com\"},\"image\":{\"@id\":\"https:\/\/tschaki.com\/#logo\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tschaki.com\/#website\",\"url\":\"https:\/\/tschaki.com\/\",\"name\":\"tschaki\",\"description\":\"Codesupport and alternatives\",\"publisher\":{\"@id\":\"https:\/\/tschaki.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tschaki.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg\",\"contentUrl\":\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg\",\"width\":1580,\"height\":500,\"caption\":\"create wordpress plugin\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#webpage\",\"url\":\"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/\",\"name\":\"Create Custom Post Type in WordPress &bull; tschaki\",\"isPartOf\":{\"@id\":\"https:\/\/tschaki.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#primaryimage\"},\"datePublished\":\"2020-11-17T14:23:59+00:00\",\"dateModified\":\"2021-11-20T15:12:03+00:00\",\"description\":\"A Custom Post Type adds another vessel to a WordPress website to capture new posts or other attachments in it.\",\"breadcrumb\":{\"@id\":\"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tschaki.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create Custom Post Type in WordPress\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#webpage\"},\"author\":{\"@id\":\"https:\/\/tschaki.com\/#\/schema\/person\/b4bf4286ba196fcd756146568acb3219\"},\"headline\":\"Create Custom Post Type in WordPress\",\"datePublished\":\"2020-11-17T14:23:59+00:00\",\"dateModified\":\"2021-11-20T15:12:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#webpage\"},\"wordCount\":210,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/tschaki.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg\",\"keywords\":[\"backend\",\"cpt\",\"custom post type\",\"faq\",\"php\",\"wordpress\"],\"articleSection\":[\"Technology\",\"Wordpress\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/tschaki.com\/#\/schema\/person\/b4bf4286ba196fcd756146568acb3219\",\"name\":\"Trent Bojett\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/tschaki.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7344f00aff4a7391a8f499ff58fc7275?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7344f00aff4a7391a8f499ff58fc7275?s=96&d=mm&r=g\",\"caption\":\"Trent Bojett\"},\"description\":\"My name is Trent Bojett, I am 28 years old and I live in Switzerland. Because of my enthusiasm for writing and the logical world (= software development) I started this blog in early 2020.\",\"sameAs\":[\"https:\/\/tschaki.com\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Create Custom Post Type in WordPress &bull; tschaki","description":"A Custom Post Type adds another vessel to a WordPress website to capture new posts or other attachments in it.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/","og_locale":"en_US","og_type":"article","og_title":"Create Custom Post Type in WordPress","og_description":"A Custom Post Type adds another vessel to a WordPress website to capture new posts or other attachments in it.","og_url":"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/","og_site_name":"tschaki","article_published_time":"2020-11-17T14:23:59+00:00","article_modified_time":"2021-11-20T15:12:03+00:00","og_image":[{"width":1580,"height":500,"url":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Written by":"Trent Bojett","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/tschaki.com\/#organization","name":"Tschaki.com","url":"https:\/\/tschaki.com\/","sameAs":[],"logo":{"@type":"ImageObject","@id":"https:\/\/tschaki.com\/#logo","inLanguage":"en-US","url":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/12\/tschaki-logo-1.png","contentUrl":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/12\/tschaki-logo-1.png","width":1022,"height":273,"caption":"Tschaki.com"},"image":{"@id":"https:\/\/tschaki.com\/#logo"}},{"@type":"WebSite","@id":"https:\/\/tschaki.com\/#website","url":"https:\/\/tschaki.com\/","name":"tschaki","description":"Codesupport and alternatives","publisher":{"@id":"https:\/\/tschaki.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tschaki.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#primaryimage","inLanguage":"en-US","url":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg","contentUrl":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg","width":1580,"height":500,"caption":"create wordpress plugin"},{"@type":"WebPage","@id":"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#webpage","url":"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/","name":"Create Custom Post Type in WordPress &bull; tschaki","isPartOf":{"@id":"https:\/\/tschaki.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#primaryimage"},"datePublished":"2020-11-17T14:23:59+00:00","dateModified":"2021-11-20T15:12:03+00:00","description":"A Custom Post Type adds another vessel to a WordPress website to capture new posts or other attachments in it.","breadcrumb":{"@id":"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tschaki.com\/en\/"},{"@type":"ListItem","position":2,"name":"Create Custom Post Type in WordPress"}]},{"@type":"Article","@id":"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#article","isPartOf":{"@id":"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#webpage"},"author":{"@id":"https:\/\/tschaki.com\/#\/schema\/person\/b4bf4286ba196fcd756146568acb3219"},"headline":"Create Custom Post Type in WordPress","datePublished":"2020-11-17T14:23:59+00:00","dateModified":"2021-11-20T15:12:03+00:00","mainEntityOfPage":{"@id":"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#webpage"},"wordCount":210,"commentCount":0,"publisher":{"@id":"https:\/\/tschaki.com\/#organization"},"image":{"@id":"https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#primaryimage"},"thumbnailUrl":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg","keywords":["backend","cpt","custom post type","faq","php","wordpress"],"articleSection":["Technology","Wordpress"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/tschaki.com\/en\/create-custom-post-type-in-wordpress\/#respond"]}]},{"@type":"Person","@id":"https:\/\/tschaki.com\/#\/schema\/person\/b4bf4286ba196fcd756146568acb3219","name":"Trent Bojett","image":{"@type":"ImageObject","@id":"https:\/\/tschaki.com\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/7344f00aff4a7391a8f499ff58fc7275?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7344f00aff4a7391a8f499ff58fc7275?s=96&d=mm&r=g","caption":"Trent Bojett"},"description":"My name is Trent Bojett, I am 28 years old and I live in Switzerland. Because of my enthusiasm for writing and the logical world (= software development) I started this blog in early 2020.","sameAs":["https:\/\/tschaki.com"]}]}},"_links":{"self":[{"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/posts\/2533"}],"collection":[{"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/comments?post=2533"}],"version-history":[{"count":5,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/posts\/2533\/revisions"}],"predecessor-version":[{"id":3046,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/posts\/2533\/revisions\/3046"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/media\/2398"}],"wp:attachment":[{"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/media?parent=2533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/categories?post=2533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/tags?post=2533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}