Prevent WordPress custom post type having a permalink page

Sometimes you have a custom post type on a site and you don’t want it to have a permalink page on the front end of the site.

If that is the case, you can use the following code to amend the post types registered arguments to make this change.

In this example we prevent WooCommerce products from having a single permalink page.

<?php
/**
 * Makes products not public to prevent them having a permalink page.
 *
 * @param array  $args      The post type args.
 * @param string $post_type The post type name.
 *
 * @return array $args The modified post type args.
 */
function hd_prevent_products_permalink_page( $args, $post_type ) {

	// if this is not the product post type.
	if ( 'product' !== $post_type ) {
		return $args;
	}

	// change the public arg.
	$args['public'] = false;

	// return the args.
	return $args;

}

add_filter( 'register_post_type_args', 'hd_prevent_products_permalink_page', 10, 2 );