Home > Software design >  Make custom post type that is rendered by plugin not queryable
Make custom post type that is rendered by plugin not queryable

Time:09-28

I'm using the WooCommerce plugin and with it comes the products post type.

For my site, I've created actual pages for my products, so I don't need /products to exist.

Usually, if I had created the post type, I would add publicly_queryable' => false to make sure the /products url didn't exist, but this post type is created by WooCommerce so unsure how to go about it? Or if it has any implications.

Trying to remove these unnecessary pages from the sitemap.

CodePudding user response:

So this answer came from this post over at the WordPress Stack Exchange site.

Add this to your theme's functions.php file and give it a try. I tested it on a clean WP install with just WooCommerce installed, and it seems to work as intended.

function change_wp_object() {
    if(post_type_exists('product')){
        $object = get_post_type_object('product');
        $object->publicly_queryable = false;
    }
}

add_action('init','change_wp_object');

I added a check to see if the 'product' post type exists (please note that your question says the post type is called 'products' but it is actually 'product'). Adding that if/condition helps eliminate errors thrown if WooCommerce wasn't installed or activated.

CodePudding user response:

If you want to exclude post_type from Yoast SEO sitemap - try this code

function sitemap_exclude_post_type( $excluded, $post_type ) {
    return $post_type === 'product';
}

add_filter( 'wpseo_sitemap_exclude_post_type', 'sitemap_exclude_post_type', 10, 2 );
  • Related