0
votes

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.

1
Remove these lines from functions.php if you are new to WP and use plugin Custom Post Type UI. Its best plugin to add new Custom PostsSudharshan Nair
I tried to use the plugin 'Custom Post Type Ui' but still couldnt get any posts created showing. I then read in the plugin support page that the plugin.. 'will not handle display of registered post types or taxonomies in your current theme. To display your data, check out CPTUI Extended'. Do I need this extension to handle the display?Steve Joiner
With that plugin you will be able create custom post type and add new posts of that new custom post type. But those posts will not automatically appear. Usually custom post types are displayed or viewed similar to default post type, which is via a template file (single.php). But if you want those custom post type to be displayed on your home page, then you will have to write custom query specific to your custom post types and display the result via loop.zipkundan
Thanks @zipkundan.. I might not have explained myself properly. Im aware that the full posts will not automatically appear but surely they should be appearing in the list of posts thats on the home page. According the tutorial they should. But it states that after giving the code samples to add to the functions.php file similar to what I have given above.Steve Joiner

1 Answers

0
votes

Try changing you function in functions.php where you are setting custom query vars.

function add_web_services_to_archives( $query ) {
    $types_array = array( 'post', 'web_services' );
    if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
        $query->set_query_var( 'post_type', $types_array );
    }
}
add_action('pre_get_posts', 'add_web_services_to_archives');

Hope this helps.