I am trying to create custom post types on a Wordpress Test Site but can not seem to get them to display. I have followed a tutorial from 'OReilly's - Wordpress the missing manual' which provides sample code to place in the 'functions.php' file and also suggests the use of a plugin - 'Custom Post Type UI'. I have tried a combination of both but with no success.
View my test site here To my understanding with the below code any posts created with my custom post 'Web Services' ,which displays correctly in the left sidebar in wordpress admin, should appear on the home page.
The code I have placed in my 'functions.php' file is as follows:
function cptui_register_my_cpts() {
/**
* Post Type: Web Services.
*/
$labels = array(
"name" => __( "Web Services", "" ),
"singular_name" => __( "Web Services", "" ),
);
$args = array(
"label" => __( "Web Services", "" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"delete_with_user" => false,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "web_services", "with_front" => true
),
"query_var" => true,
"supports" => array( "title", "editor", "thumbnail", "excerpt" ),
"taxonomies" => array( "category", "post_tag" ),
);
register_post_type( "web_services", $args );
}
add_action( 'init', 'cptui_register_my_cpts' );
function add_web_services_to_archives( $wp_query ) {
$types_array = array( 'post', 'web_services' );
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
set_query_var( 'post_type', $types_array );
}
}
add_action('pre_get_posts', 'add_web_services_to_archives');
I have gone into 'Settings/Permalinks' to save changes which the tutorial advises to do but still no joy.
So to reiterate, in the same way when you add a normal post it automatically appears on the home page. I would like the same to happen when I add a custom post.
functions.php
if you are new to WP and use plugin Custom Post Type UI. Its best plugin to add new Custom Posts – Sudharshan Nair