Apr
24
2019

How to exclude WordPress custom post types from Yoast’s HTML sitemap

WordPress website header - WP logo

If you have a WordPress website and have looked at improving its search engine friendliness, you will now doubt have come across the Yoast SEO plugin. One of the additional tasks you can do to improve your WordPress website’s SEO is to add a HTML sitemap to list all of the pages, posts and custom post types for search engines to find more readily.

Yoast has a handy guide to creating a HTML sitemap for WordPress already, but you may find that it needs adapting if you don’t want to list certain custom post types in your website’s sitemap.

A good example of this was a recent change for a WordPress SEO audit client of ours, who had a custom post type which recorded information from a quote system, and which was not required to be found by search engines (of course, we also hid the content another way!).

So, here’s our guide to excluding specific custom post types from the Yoast HTML sitemap:

1. Create a sitemap page template for your WordPress theme

The first step is to create a sitemap page template for your WordPress theme. Create a file called page-sitemap.php in your WordPress theme’s directory (e.g., wp-content/themes/your-theme/page-sitemap.php) and add the following:

 

<?php
/* Template name: Sitemap */
get_header();
?>

<h2 id="pages">Pages</h2>
<ul>
<?php
wp_list_pages( array( 'exclude' => '', 'title_li' => '' ) );
?>
</ul>
<?php
foreach( get_post_types( array('public' => true) ) as $post_type ) {
if ( in_array( $post_type, array('post','page','attachment') ) )
continue;
$pt = get_post_type_object( $post_type );
if ( $post_type != 'custom-post-type' ) {
echo '<h2>'.$pt->labels->name.'</h2>';
echo '<ul>';
query_posts('post_type='.$post_type.'&posts_per_page=-1');
while( have_posts() ) {
the_post();
echo '<li><a href="'.get_permalink().'"">'.get_the_title().'</a></li>';
}
echo '</ul>';
}
}
?>

<?php
get_footer();
?>

You will find that you need to adapt the HTML to ensure that the new sitemap page displays like other pages in your WordPress website. Depending on the theme you have enabled, you may not need the get_footer() and get_header() statements in this file.

2. Exclude the custom post types you don’t want appearing in search engines

For each custom post type you don’t want to appear in your HTML sitemap, you can adapt the line above:

<?php
foreach( get_post_types( array('public' => true) ) as $post_type ) {
if ( in_array( $post_type, array('post','page','attachment') ) )
continue;
$pt = get_post_type_object( $post_type );
if ( $post_type != 'custom-post-type' && $post_type != 'another-custom-post-type' ) {
echo '<h2>'.$pt->labels->name.'</h2>';
echo '<ul>';
query_posts('post_type='.$post_type.'&posts_per_page=-1');
while( have_posts() ) {
the_post();
echo '<li><a href="'.get_permalink().'"">'.get_the_title().'</a></li>';
}
echo '</ul>';
}
}
?>

This snippet would ensure that the custom post types with machine names “custom-post-type” and “another-custom-post-type” are omitted from the Yoast HTML sitemap. If you’re not sure that this value is for your post types, try using the get_post_type() function within the loop above.

3. Assign the sitemap page template to a sitemap page

Next, you will need to create a page called “Sitemap” within your WordPress website. Once that’s done, assign your new Sitemap template using the “Page template” dropdown in the Attributes block (usually displayed in the right-hand column of the screen on larger screens).

4. Link to your sitemap page

Finally, now that you’ve created your custom HTML sitemap page for your WordPress website, be sure to link it from the footer of your website. Depending on the WordPress theme you have enabled, you may be able to do this via the Appearance > Menus screen in the backend when logged in as a site administrator, or by editing your theme’s footer.php file.

If you need any help implementing this on your own website, contact us at hello@richardcarterconsultancy.com and talk to us about our WordPress consultancy services.