tschaki
create wordpress plugin
Home » Create Custom Post Type in WordPress

Create Custom Post Type in WordPress

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.

Register and create Custom Post Type

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.

To register the new Custom Post Type the following code is added to the functions.php of the active theme or plugin:

/*
* 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 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();
	the_title();
	the_content();
}

In the above example, the title and content of the faq post type found in WordPress are output.

In addition, WordPress now displays the new Custom Post Type in the menu in the backend.

Source: WordPress Codex register_post_type( )

Downloads

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