Hide a Custom Post Type’s Single and Archive Pages in WordPress

You may find that you need to hide a custom post types single or archive pages or even both in WordPress. This may be the case if you are outputting a custom loop of cpts and don’t want duplicate content or discoverable pages which you don’t want published.

<?php
/*
Plugin Name: Event Custom Post Type
Plugin URI: http://wpbeaches.com/create-custom-post-types-in-genesis-child-theme-in-wordpress/
Description: Custom Post Types for Event
Author: Neil Gee
Version:1.0.0
Author URI:http://wpbeaches.com
*/
/*
This code is a plugin to create a Custom Post Type in WordPress, it can be used with any WordPress theme.
The action initialises the function below it.
This example uses the term 'Events' as its name, a search and replace will allow any name to be used, making sure plural and singular versions of the name are replaced.
Also replace the name in 'rewrite' and in the 'register_post_type' function.
For non-Genesis themes the 'genesis-cpt-archives-settings' can be removed from the supports array.
To activate this as a plugin just add to wp-contents/plugins and activate in Dashboard
This doesn't use all the labels and arguments possible but includes the main ones, you can see more here - https://codex.wordpress.org/Function_Reference/register_post_type
*/
add_action( 'init', 'themeprefix_create_custom_post_type' );
function themeprefix_create_custom_post_type() {
$labels = array(
'name' => __( 'Event' ),
'singular_name' => __( 'Event' ),
'all_items' => __( 'All Events' ),
'add_new' => _x( 'Add new Event', 'Events' ),
'add_new_item' => __( 'Add new Event' ),
'edit_item' => __( 'Edit Event' ),
'new_item' => __( 'New Event' ),
'view_item' => __( 'View Event' ),
'search_items' => __( 'Search in Events' ),
'not_found' => __( 'No Events found' ),
'not_found_in_trash' => __( 'No Events found in trash' ),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => false, // Set to false hides Archive Pages
'menu_icon' => 'dashicons-admin-users', //pick one here ~> https://developer.wordpress.org/resource/dashicons/
'rewrite' => array( 'slug' => 'event' ),
'taxonomies' => array( 'category', 'post_tag' ),
'query_var' => true,
'menu_position' => 5,
'publicly_queryable' => false, // Set to false hides Single Pages
'supports' => array( 'genesis-cpt-archives-settings', 'thumbnail' , 'custom-fields', 'excerpt', 'comments', 'title', 'editor')
);
register_post_type( 'event', $args );
}

In the above gist in the $args, ‘has_archive‘ is set to false which hides the archive pages and publicly_queryable is also set to false which hides the single pages.

Make sure to flush permalinks after and these pages will no longer be visible.

Leave a Comment





%d bloggers like this: