Custom Post Type in WordPress

Einer von vielen Vorzügen einer WordPress Webseite besteht darin, dass sich die Webseite beliebig mit Inhalt erweitern lässt. Eine der besten Funktionalitäten ist auf jeden Fall die Erweiterung via sogenanntem Custom Post Type.

Register new Custom Post Type

A Custom Post Type extends a WordPress web page by another container for new posts. For example, you can use it to collect references, books or movies to display them centrally on the website. In this example the page is extended by a FAQ container.

To register the new Custom Post Type the following code is inserted into the functions.php of the active theme

* Creating a function to create our CPT
function faq_custom_post_type() {
    // Set UI labels for Custom Post Type
    $labels = array(
        'name' => _x( 'FAQ', 'FAQ', 'twentytwenty' ),
        'singular_name' => _x( 'FAQ', 'FAQ', 'twentytwenty' ),
        'menu_name' => __( 'FAQ', 'twentytwenty' ),
        'all_items' => __( 'Alle FAQs', 'twentytwenty' ),
        'view_item' => __( 'FAQ anzeigen', 'twentytwenty' ),
        'add_new_item' => __( 'Neues FAQ', 'twentytwenty' ),
        'add_new' => __( 'Neues FAQ', 'twentytwenty' ),
        'edit_item' => __( 'FAQ bearbeiten', 'twentytwenty' ),
        'update_item' => __( 'FAQ aktualisieren', 'twentytwenty' ),
        'search_items' => __( 'Suche FAQ', 'twentytwenty' ),
        'not_found' => __( 'Nicht gefunden', 'twentytwenty' ),
        'not_found_in_trash' => __( 'Nicht gefunden im Papierkorb', 'twentytwenty' )
    // Set other options for Custom Post Type
    $args = array(
        'label' => __( 'faqs', 'twentytwenty' ),
        'description' => __( 'FAQs', 'twentytwenty' ),
        'labels' => $labels,
        // Features this CPT supports in Post Editor
        'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies' => array( 'category' ),
        // A hierarchical CPT is like Pages and can have parent and child items. A non-hierarchical CPT is like Posts.
        'hierarchical' => false,
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => true,
        'show_in_admin_bar' => true,
        'menu_position' => 5,
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'show_in_rest' => true,
    // Registering your Custom Post Type
    register_post_type( 'faq', $args );
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
add_action( 'init', 'faq_custom_post_type', 0 );

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 post_type.

Output articles of the new Custom Post Type

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.

$args = array( 'post_type' => 'faq', 'posts_per_page' => 10 );
$the_query = new WP_Query( $args ); 
// Fetch 10 posts with post_type faq
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

Trent Bojett

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.

Add comment


Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.

Most popular

Most discussed