Custom Post Type in WordPress

Woocommerce Plugin by plural.software

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.

Neuer Custom Post Type registrieren

Ein Custom Post Type erweitert eine WordPress Webseite um ein weiteres Gefäss um neue Beiträge darin zu erfassen. Beispielsweise kann man damit Referenzen, Bücher oder Filme sammeln um diese dann zentralisiert auf der Webseite ausgeben. In diesem Beispiel wird die Seite um ein FAQ Gefäss erweitert.

Um den neuen Custom Post Type zu registrieren wird folgender Code in die functions.php des aktiven Themes eingefügt:

/*
* 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 );

Nach Einfügen des Codes können sämtliche Parameter im Code angepasst werden um beispielsweise Name, Taxanomy oder Anzeigeicon im Backend zu verändern. Nun können im WordPress Backend neue Beiträge erfasst werden, welche unter dem neuen ‚post_type‘ abgespeichert werden.

Beiträge des neuen CPT ausgeben

Um die angelegten Beiträge des neu erstellten Custom Post Type ausgeben zu können wird eine Abfrage an die Datenbank gestellt, welche nur die Beiträge mit dem passenden Post Type einliest.

<?php $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(); ?>
    // Output all 10 posts with whileloop
    <h2><?php the_title(); ?></h2>
    <div class="entry-content">
        <?php the_content(); ?> 
    </div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>