WordPress Custom Post Type

| By Webner

WordPress Custom Post Type

In WordPress every single content is generally called a post, even post is also a post type in wordpress. There are many predefined post types like post, page, attachment and many more. We can also create WordPress custom post types if needed.

Function to register custom post type:

register_post_type( $post_type, $args );

Parameter description:

$post_type:(string) (Required) Post type. (max. 20 characters, cannot contain capital letters or spaces)
Default: None
$args: (Array) (Optional) Post type arguments array for defining post type definition.
Inside it we can pass several different arguments listed below:
Label: (string) (Optional) A plural descriptive name for the post type.
Labels: (Array) (Optional) An array of labels for this post type like name, singular_name, add_new, edit_item etc.
Description: (string) (optional) A short descriptive summary of what the post type is.
Default: blank
Public: (boolean) (optional) set true if this post type is visible to authors (show_in_nav_menus, show_ui) and readers (exclude_from_search, publicly_queryable) Default: false
Taxonomies: (array) (optional) An array of registered taxonomies like category or post_tag that will be used with this post type. Default: no taxonomies
Supports: (array/boolean) (optional) define what common features will be added.
Default: title and editor.

Note: This parameter (Supports) is an alias for calling add_post_type_support() directly.

There are many more parameters for $args. You can check all of them at: https://codex.wordpress.org/Function_Reference/register_post_type


If we have to create a testimonial type post to show clients feedback on the frontend with client picture, below is code for this:

// Set post type
$post_type = “testimonial”; 
// Set post type arguments
$args = [
“label” => “Testimonials”,
“labels”  => [
'name' => ‘Testimonials’,
'singular_name' => ‘Testimonial’,
'add_new' => ‘Add Testimonial’,
'add_new_item' =>  ‘Add Testimonial’,
'edit_item' => ‘Edit Testimonial’,
'view_item' => ‘View Testimonial’,
‘Featured_image’ => ‘Client Picture’,
'Set_featured_image' => ‘Set Client Picture’,
'remove_featured_image' => ‘Remove Client Picture’,
'Use_featured_image' => ‘Use Client Picture’,
“Public” => true,
“Supports” => [ 'title', ‘editor’,’thumbnail’
register_post_type( $post_type, $args ); 

Above code will add Testimonials menu in admin to manage Testimonial posts.
Now we have to fetch Testimonials in the frontend, use below code:

<?php $args = array(
 'posts_per_page'   => -1,
 'orderby'          => 'date',
 'order'            => 'DESC',
 'post_type'        => testimonial,
 'post_status'      => 'publish',
$testimonials = get_posts( $args ); ?>
<?php foreach ( $testimonials as $testimonial ) : setup_postdata( $testimonial ); ?>
<?php the_post_thumbnail(); ?>
<h3><? php the_title(); ?></h3>
<p><?php the_content(); ?></p>
<?php endforeach; 

Leave a Reply

Your email address will not be published. Required fields are marked *