Custom Post Type Archives

In addition to getting sermons by speaker, I would also like to be able to access them by month. Unfortunately, there’s currently no built in method to do so. A bit of poking around revealed the Custom Post Type Archives Plugin.

Setting up the plugin was easy enough. Unzip the file into the plugin folder and activate it, then go to the settings page and select the Sermon type. Go to the Permalinks settings page to flush the rewrite rules.

Keeping the other settings at their default values, sermons can now be accessed at domain/site/post-type/year/month (substitute the names with their proper values). However, speakers no longer show up. I realized that it’s using one of the default template files, so I changed the template pattern setting to {POST_TYPE}-template.php. This causes it to look for sermon-template.php, which will run the custom sermon loop.

Unfortunately, this caused the undesired side effect of overwriting the query with the custom query for normal sermon display. Thus the year and month information is lost. The code for sermon-template.php had to be changed.

When accessing the normal page, the query comes with the page name. This value isn’t wanted, but I could use it to differentiate between the listings page and the archives.

global $wp_query;
$existing = $wp_query->query;

if(isset($existing['pagename']))
    $args = array('post_type'=>'sermon','posts_per_page'=>5,'paged'=>get_query_var('paged'));
else
    $args = array_merge($wp_query->query,array('post_type'=>'sermon','paged'=>get_query_var('paged')));
query_posts($args);

The missing posts_per_page argument in the else clause is by design. There shouldn’t be more than 5 sermons in each month anyway. Also, despite what the documentation says, paging doesn’t work in the plugin.

Custom Post Type Archives Part 2

Advertisements

4 thoughts on “Custom Post Type Archives”

    1. Hi franklynjones,

      Things have changed along the way. Did you go over the rest of the posts in the series on custom post type archives? Perhaps one of the refinements there will work for you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s