Creating Custom Post type in WordPress – Tutorial Series

 Creating Custom Post type in WordPress – Tutorial Series

Creating custom articles in WordPress is not difficult, you just need to understand when and how to use them in your website

There are two ways to achieve creating custom post types in WordPress and we will learn about them today in this tutorial.

What is the type of custom posts in WordPress?

Custom post types are nothing more than a normal post with a different value for the post type in the database. Regular posts type is post, pages use page, attachments use attachments, and so on

You can use the custom post type if you need to separate different content from the default posts in regular WordPress; For example, if you want to create post pages called sites, galleries or cities, and have their own post page and blog page.

Create custom post type without plugin using PHP code

All you need is to head to function.ph p in your WordPress theme file and paste the following code, you just need to change the so-called textdomain identifier of the theme, if you don’t know what this is, go to your theme and open the style.css file and you will see a line called textdomain and next to it you will see what is your WordPress text domain. And you also need to change the name as you like:

/*
* Creating a function to create our CPT
*/
 
function custom_post_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Locations', 'Post Type General Name', 'twentytwenty' ),
        'singular_name'       => _x( 'Location', 'Post Type Singular Name', 'twentytwenty' ),
        'menu_name'           => __( 'Locations', 'twentytwenty' ),
        'parent_item_colon'   => __( 'Parent Location', 'twentytwenty' ),
        'all_items'           => __( 'All Locations', 'twentytwenty' ),
        'view_item'           => __( 'View Location', 'twentytwenty' ),
        'add_new_item'        => __( 'Add New Location', 'twentytwenty' ),
        'add_new'             => __( 'Add New', 'twentytwenty' ),
        'edit_item'           => __( 'Edit Location', 'twentytwenty' ),
        'update_item'         => __( 'Update Location', 'twentytwenty' ),
        'search_items'        => __( 'Search Location', 'twentytwenty' ),
        'not_found'           => __( 'Not Found', 'twentytwenty' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwenty' ),
    );
     
// Set other options for the custom post type
     
    $args = array(
        'label'               => __( 'Locations', 'twentytwenty' ),
        'description'         => __( 'Location news and reviews', '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. 
         /* 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( 'Locations', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );

Create custom posts in WordPress with a plugin

Now go to Add- ons , add new and find Pods, click install and activate it

Creating Custom Post Types in WordPress using a Plugin pods

Once this is done, go to the Pods Control Panel and click on “ Add New ” and then “ Create New ”

pods 1

As you can see above, we just created custom post types in WordPress called Site

Display custom posts in WordPress on the home page

One advantage of using custom post types is that they keep custom content types separate from your regular posts. However, if you want to display it within your regular post, you can do so by adding this code to your theme’s functions.php file:

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
 
function add_my_post_types_to_query( $query ) {
    if ( is_home() && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'locations' ) );
    return $query;
}

Display custom posts in WordPress using the . plugin

The first thing you need to do is install and activate the Ultimate Posts Widget. When activated, simply go to Appearance » Widgets and drag and drop the Ultimate Posts widget into a sidebar.

Configure the tool by selecting the options you want and selecting the custom post type. Next, save your changes and see the tool in action on your website.

Display custom posts in WordPress without a plugin

Now we need to write some code with php and html also css to format the articles display then make a shortcode to display the post type posts wherever we want in our WordPress site

Go to your WordPress template and create a new file, call it myposts-grid.php and paste the following code there:

<?php 
$args = array( 'post_type' => 'locations', 'posts_per_page' => 10 );
$the_query = new WP_Query( $args ); 
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post();  ?>
<?php 
if (has_post_thumbnail( ) ): 
 $url = wp_get_attachment_url( get_post_thumbnail_id( ), ' ' ); ?>
 <img src="<?php echo $url ?>" class="card-img-top" alt=" <?php the_title();?> ">
 <?php endif; ?>
<h5><?php the_title(); ?></h5>
<?php wp_reset_postdata(); ?>
<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

The next step is to call this query in Functions.php to create your shortcode. Now go to Functions.php File and paste this code there:

<?php 
function wpmrj3_locations_shortcode( $attr ) {
    ob_start();
    get_template_part('myposts/grid');
    return ob_get_clean();
}
add_shortcode( 'location-post', 'wpmrj3_locations_shortcode' );

Now go to your WordPress dashboard and paste this code where you want to display the post type posts

[location-post]

Create a template for custom posts published in WordPress

Also here you have two options to create a template for custom posts published in WordPress, one of which is using a plugin like Elementor , as well as with code

Create a WordPress Custom Post Template with Elementor

You need to get Elementor Pro to do this, once you have Elementor and Elementor Pro installed on your site, go to Templates » Add New , choose Single Post Type, assign the new template to Post Type Sites and give it a name as you want and hit Create Template

elementor pro custom post type

Once you are done designing your new post type template, click save and add some view conditions as you can see

elementor pro custom post type 4

That’s it, Elementor has done it all

Create a template for custom articles using PHP without a plugin

To do this, we first need to go to the template folder and open it, create a new file that you should name

single-locations.php . Don’t forget to replace the file name with the name of the custom article you created.

Once done, open the single-locations.php file and paste the following code:

 <?php get_header(); ?>
<article class="body-entry post" <?php post_class(); ?> id="post-<?php the_ID(); ?>">
  
          <div class="post-content">
                 <h1><?php the_title(); ?></h1>
                <?php  
                if (has_post_thumbnail( ) ): 
                 $url = wp_get_attachment_url( get_post_thumbnail_id( ), ' ' ); ?>
                 <img src="<?php echo $url ?>" class="card-img-top" alt=" <?php the_title();?> ">
                 <?php endif;  ?>

                 <?php 
                 if(have_posts() ): while(have_posts() ): the_post(); 
                 the_content(); 
                 endwhile; else : endif;?>
        </div>
</article>
<?php get_footer(); ?>

All you have to do now is write some HTML and CSS code to make the template template even better. In the end, you should now go to the WordPress Dashboard » Settings » Permalink » Save Changes

That’s all you need to know about creating custom post types in WordPress and now the tutorial is just over

Learn Also: Create Custom Category in WordPress – No Hard Work