Support » Developing with WordPress » Is it possible to have a post type with an archive but no single?

  • Jason Adams

    (@jason_the_adams)



    I know when registering a post type it’s possible to set whether or not a post type has an archive using the has_archive parameter. Unfortunately, there’s no has_single equivalent. I’ve been trying to figure out if there’s any way, otherwise, of flagging a post type to have an archive but not single.

    I could always just set the single to 404, but plugins such as WordPress SEO will still place the single pages within the sitemap, which is bad juju.

    The workaround I’ve done is to make a page template that acts as the archive and set the public to false and show_ui to true. This works, but isn’t great as the user may go to make more than one and, moreover, getting the archive url isn’t nearly as easy.

    Thanks for any ideas!

Viewing 3 replies - 1 through 3 (of 3 total)
  • here’s a cool little hack i whipped up. You can put this in your themes functions.php file.

    I hooked into the template_include action and if it is single & the custom post type is equal to ‘custom_post_type’ (change this to whatever yours is) get the 404-page template instead of the correct one. If it isn’t, return the template as it does normally.

    add_action( 'template_include', 'if_custom_post_type_single' );
    
    function if_custom_post_type_single( $template ) {
    	$queried_post_type = get_query_var('post_type');
    	if ( is_single() && 'custom_post_type' == $queried_post_type ) {
    		$new_template = locate_template( array( '404.php' ) );
    		if ( ! empty( $new_template ) ) {
    			return $new_template;
    		}
    	}
    
    	return $template;
    }
    • This reply was modified 1 year, 11 months ago by  Storm Rockwell. Reason: checking off follow-up replies
    Jason Adams

    (@jason_the_adams)

    Hey @stormrockwell!

    I appreciate the suggestion, unfortunately it doesn’t quite fit what I’m looking for. That would just load the 404.php for the CPT single, which has the appearance of working. But plugins, such as WordPress SEO, are still going to include those in the sitemap, which then presents a bunch of broken links. Not good.

    I’m hoping to figure out if there’s a way to mark a CPT as not having a single, while still somehow achieving an effective archive. Like I said, so far my only known way of doing this is to set 'rewrite' => false and use a page template.

    Thanks, again!

    Good point. I don’t think WordPress has this functionality and would require a hacky solution. I would suggest opening a ticket in trac: https://core.trac.wordpress.org/ with your proposed enhancement to CPTs

    possibly adding a parameter like ‘has_single’?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Is it possible to have a post type with an archive but no single?’ is closed to new replies.