• Resolved kees78

    (@kees78)


    Hi,

    We have created a custom post type and for some reasons it has to be set to public. Yet we dont want it to be indexed or showed in the sitemap.

    I have used the filter provided on the website:

    function sitemap_exclude_post_type( $excluded, $post_type ) {
        return $post_type === 'my-custom-post-type';
    }
    
    add_filter( 'wpseo_sitemap_exclude_post_type', 'sitemap_exclude_post_type', 10, 2 );

    Somehow, the custom post type is still displayed in the sitemap.

    In the Yoast settings, the CPT is available. But i want to exclude it from the sitemap programmatically.

    What am i missing here?

    Kind regards, Kees

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Maybellyne

    (@maybellyne)

    Hello Kees,

    Thanks for using the Yoast SEO plugin. If excluding the CPT using the filter doesn’t work, please do so in the Yoast SEO settings UI.

    Thread Starter kees78

    (@kees78)

    hi,

    i know i can turn it off in the settings, but i want to do it programmatically for reasons of automation. Somehow the filter provider does not work. How come?

    Thread Starter kees78

    (@kees78)

    Ok for anybody running in the same problem: i found out that the sitemap option is stored in the options database under wpseo_titles. So if you want to programmatically remove your custom post type from indexation, use this function:

    function remove_sitemap_cpt() {
    	$yoast_settings = array();
    	$cpt = 'your-custom-cpt';  //fill in your cpt
    	if(!empty(get_option('wpseo_titles'))){	
    		$yoast_settings = get_option('wpseo_titles');
    	}
    	if(isset($yoast_settings['noindex-'.$cpt]) && empty($yoast_settings['noindex-'.$cpt])){
    		$yoast_settings['noindex-'.$cpt] = true;
    		update_option('wpseo_titles',$yoast_settings);
    	}
    }
    add_action('admin_head','remove_sitemap_cpt');
    • This reply was modified 11 months, 4 weeks ago by kees78.
    Plugin Support devnihil

    (@devnihil)

    @kees78 Thanks for posting your solution. If you should need any further assistance, please do not hesitate to let us know.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Yoast not excluding cpt from sitemap’ is closed to new replies.