How to Create Custom Post Types in WordPress

We can create your own custom post types and call them whatever we want. For example if you run a Loveseats website, then you would probably want to create a Loveseats post type. This post type can have different custom fields and even its own custom category structure. Other examples of post types are: Portfolio, Testimonials, Products, etc.

1. First register post type

function fullstack_cpt() {
    $args = array(
        'public' => true,
        'label'  => 'Loveseats Item',
         'menu_icon' => 'dashicons-category',
		'supports' => array( 'title', 'editor', 'thumbnail' )
    );
    register_post_type( 'loveseats', $args );
    
}
add_action( 'init', 'fullstack_cpt' );

2. Then query it!

$args = array( 'posts_per_page' => -1, 'post_type'=>'loveseats','orderby' => 'date','order' => 'DESC'  ); 

 $_posts = new WP_Query( $args );
        while ( $_posts->have_posts() ) {
                $_posts->the_post();
                // thumbnail
                get_the_post_thumbnail( $post->ID, 'size' );
                //title
                the_title();
                // excerpt
                the_excerpt();
                // permalink
                the_permalink();
                // meta query
                get_post_meta( get_the_ID(), '_meta_data', true );
              }
wp_reset_postdata();

Leave a Reply

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

Name *

Available for work