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() ) {
                // thumbnail
                get_the_post_thumbnail( $post->ID, 'size' );
                // excerpt
                // permalink
                // meta query
                get_post_meta( get_the_ID(), '_meta_data', true );

Leave a Reply

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

Name *

Available for work