WordPress Sitemap with Custom Post Types

Sitemaps offer visitors a great overview of a site’s content, and WordPress allows you to create them with ease. Joost de Vaulk shows you how to make one version of a sitemap. However, it doesn’t include an option for custom post types, which have become more popular since WordPress 3.0 came out. Here’s how to make that happen, building on Joost’s example.

The Code

You’ll want to start out with Joost’s code because it works perfectly, and will give you just what you need for your posts and pages. Use this code for for your custom post type:

You’ll see a few things happening here:

<h2 id="my-post-type">My Post Type</h2>
<ul>
	<?php
	$terms = get_terms( 'my_taxonomy', 'orderby=name' );
	foreach ( $terms as $term ) {
	echo '<li><h3>' . $term->name . '</h3>';
	echo '<ul>';
	$args = array(
		'post_type' => 'my_post_type',
		'posts_per_page' => -1,
		'tax_query' => array(
			array(
				'taxonomy' => 'my_taxonomy',
				'field' => 'slug',
				'terms' => $term->slug
			)
		)
	);
	$new = new WP_Query($args);
	while ( $new->have_posts() ) {
	$new->the_post();
	echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
	}
	echo '</ul>';
	echo '</li>';
	} ?>
</ul>
  • We’re setting up a basic unordered list, based on our custom taxonomy and listing posts in our custom post type by title.
  • In the second chunk of code, we’re using a new WP Query to query the posts for our custom post type and custom taxonomy.

You’ll need to change the code in a few key places.

$terms = get_terms( 'my_taxonomy', 'orderby=name' );

Change the above code the your custom taxonomy name.

'post_type' => 'my_post_type',

Change the above code the your custom post type name.

'taxonomy' => 'my_taxonomy',

Change the above code the your custom taxonomy name.

Wrapping It Up

That does it. Now, whenever you add posts to your post type and taxonomy, it gets automatically added to the sitemap. Nice! Follow the project on GitHub or download the files from GitHub. Huge thanks to Joost for a great code snippet! And thanks to Joeleen Kennedy for helping me figure this one out.

6 thoughts on “WordPress Sitemap with Custom Post Types

  1. awesome! exactly what i was looking for. thanks!
    Is there a way to organize my_taxonomy (portfolio_category) in a parent-child outline instead of alphabetical?

      1. I’m also interested in a parent/child type archive outline for Custom Post Types. Did anything ever come of this? It would be great to be able to customize the styles for different levels of parent pages as well as well as have some depth in the levels.

        BTW, the code you referenced above in this post doesn’t seem to be showing and instead of returning nothing. So I’m confused!

        1. Hi Micah,

          Fixed now. Sorry about that. The embed code from Pastebin got messed up and was not embedding properly. Check it out, and let me know if you have any questions.

          I still haven’t figured out the parent-child relationship for taxonomies.

Comments are closed.

%d bloggers like this: