Skip to main content
Tweeted twitter.com/StackWordPress/status/1230145165731516419
edited tags
Link
Stephen Harris
  • 32.6k
  • 6
  • 84
  • 118
Source Link
urok93
  • 4k
  • 12
  • 65
  • 104

Slug for custom post type archive

I created a new custom post type 'Projects' and want the archive of all posts of this type to be available at mysite.com/projects. At the moment all project single posts are shown with a slug as follows mysite.com/projects/project-title, but when I go to mysite.com/projects I get a 404.

Here is how I built the custom post type:

   /* Create the Project Custom Post Type ------------------------------------------*/
 function create_post_type_project() 
 {
$labels = array(
    'name' => __( 'Projects' ),
    'singular_name' => __( 'Project' ),
    'add_new' => __('Add New'),
    'add_new_item' => __('Add New Project'),
    'edit_item' => __('Edit Project'),
    'new_item' => __('New Project'),
    'view_item' => __('View Project'),
    'search_items' => __('Search Project'),
    'not_found' =>  __('No project found'),
    'not_found_in_trash' => __('No project found in Trash'), 
    'parent_item_colon' => ''
  );
  
  $args = array(
    'labels' => $labels,
    'public' => true,
    'exclude_from_search' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'query_var' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => null,
    // Uncomment the following line to change the slug; 
    // You must also save your permalink structure to prevent 404 errors
    'rewrite' => array( 'slug' => 'projects' ),
    'has_archive' => true,
    'supports' => array('title','editor','thumbnail'),
    
  ); 
  
  register_post_type(__( 'project' ),$args);
    }