Tag Archives: Custom Post Type

[WordPress] Creating custom post type and converting/changing post types of your post/page

Creating a custom post type is as simple as tweaking the code, saving it to your functions.php file or if you prefer to have it not bound to your theme then saving it to a code snippet plugin.

Follow this guide here. If you prefer to have the posts be categorized and taggable, then use the modified code below:

/* Custom Post Type Start */
function create_posttype_services() {
    register_post_type( 'service',
    // CPT Options
    array(
      'labels' => array(
       'name' => __( 'Services' ),
       'singular_name' => __( 'Service' )
      ),
      'public' => true,
      'has_archive' => false,
      'rewrite' => array('slug' => 'services'),
     )
    );
    }
    // Hooking up our function to theme setup
    add_action( 'init', 'create_posttype_services' );
    /* Custom Post Type End */
    
    /*Custom Post type start*/
    function cw_post_type_services() {
    $supports = array(
    'title', // post title
    'editor', // post content
    'author', // post author
    'thumbnail', // featured images
    'excerpt', // post excerpt
    'custom-fields', // custom fields
    'comments', // post comments
    'revisions', // post revisions
    'post-formats', // post formats
);
    $taxonomies = array(
    'category', // post title
    'post_tag', // post content
);
    $labels = array(
    'name' => _x('Services', 'plural'),
    'singular_name' => _x('Service', 'singular'),
    'menu_name' => _x('Services', 'admin menu'),
    'name_admin_bar' => _x('Services', 'admin bar'),
    'add_new' => _x('Add New', 'add new'),
    'add_new_item' => __('Add New Service'),
    'new_item' => __('New Services'),
    'edit_item' => __('Edit Services'),
    'view_item' => __('View Services'),
    'all_items' => __('All Services'),
    'search_items' => __('Search Services'),
    'not_found' => __('No Services found.'),
    );
    $args = array(
    'supports' => $supports,
    'taxonomies' => $taxonomies,
    'labels' => $labels,
    'public' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'services'),
    'has_archive' => true,
    'hierarchical' => false,
    );
    register_post_type('service', $args);
    }
    add_action('init', 'cw_post_type_services');
    /*Custom Post type end*/

You can simply change “service” and “services” to the singular and plural of your choice. I’ve bolded the only change I did.

Now that you’ve got your custom post added and working, like me you probably wanted to change some of your current posts. There’s a great plugin called Post Type Switcher, however it seems to not be getting updated anymore and failed to work. The next option is to use a SQL Query. Log in to your phpMyAdmin, head over to your wp_posts, find SQL on the menu and run the below command. Look further into this article for more information.

UPDATE wp_posts SET post_type = 'service' WHERE post_type = 'post'

Links & References

How to Create Custom Post Type in WordPress (Easy Guide)
https://www.cloudways.com/blog/wordpress-custom-post-type/

How to convert/change post type in WordPress – QuadLayers
https://quadlayers.com/change-post-type-in-wordpress/