Surf the Dream A discourse of links and articles from Justin Avery

Using different permalink structures for custom post types

I’m in the process of shifting from Responsive Design site from the current CMS, Squiz Matrix, over on to WordPress. There are a number of reasons for the move which I might put into a post at some point, but I’ve been enjoying the freedom to create Custom Post Types for the different types of content that are specific to my site and its content.

Different Custom Post Types

There are a few sections to the responsive design site and each of them requires a different set of content items and will have a different design layout as well.

The different areas I’ve set up so far include

These have all been generated using a plugin called CPTUI, but there are a number of different Custom Post Type generator plugins that I’ve come across. The great thing about using this one has been that it provides me with the code that I could drop into the Functions.php file instead of relying on the plugin.

Custom Post Types in Plugin vs Custom Posts in functions.php

Being new to the world of custom post types I took to twitter with the question of which way I should go… keep it with a plugin (which is a database based Custom Post Type) or keep it in the functions.php file (which would be a file based Custom Post Type).

There are pros and cons for both sides.

Plugin

Pros

Cons

funtions.php

Pros

Cons

Should I stay or should I go?

I ended up going with both options. I create the Custom Post Type in the plugin to start with before copying and pasting the code to the functions.php file and removing the plugin version.

Now that I have the generic layout for a custom post though I’m working primarily in the functions.php file now.

Custom Post Type Settings and Permalinks

The scope of this article isn’t to explain how to set them up, but more about a specific issue I had when I was trying to configure the permalink structures. I have the functions.php code below that you can use if you want but we’re focussing on one specific aspect that is related to permalinks and the URL structure.

These are the URL patterns I was trying to achieve

Everything was relatively flat to the site URL with the exception of the News posts which I wanted to keep as a date-based URL. When I updated the Permalinks in the WP Admin I added the following to the Customer Structure /news/%year%/%monthnum%/%postname%/ which worked a treat… however when I also viewed one of the other Custom Post Types it was also prefixing news to the URL leaving me with

Bugger.

It turns out the first thing I tried worked which hardly every happens.

The fix was to change this

"rewrite" => array( "slug" => "podcasts", "with_front" => true ),

to this

"rewrite" => array( "slug" => "podcasts", "with_front" => false ),

By updating the “with_front” to false it removed the news and I had the URL’s I was after. Woohoo!

Here’s the full code for the registering of the Custom Post Type for podcasts.

add_action( 'init', 'CPT_register_my_cpts_podcasts' );
function CPT_register_my_cpts_podcasts() {
	$labels = array(
		"name" => __( 'Podcasts', 'rwd-is' ),
		"singular_name" => __( 'Podcast', 'rwd-is' ),
		);

	$args = array(
		"label" => __( 'Podcasts', 'rwd-is' ),
		"labels" => $labels,
		"description" => "Responsive Design Podcasts run in two typical show types... ones with a guest and ones without. ",
		"public" => true,
		"publicly_queryable" => true,
		"show_ui" => true,
		"show_in_rest" => false,
		"rest_base" => "",
		"has_archive" => true,
		"show_in_menu" => true,
				"exclude_from_search" => false,
		"capability_type" => "post",
		"map_meta_cap" => true,
		"hierarchical" => false,
		"rewrite" => array( "slug" => "podcasts", "with_front" => false ),
		"query_var" => true,

		"supports" => array( "title", "editor", "thumbnail", "excerpt" ),
		"taxonomies" => array( "category", "post_tag", "People" ),
			);
	register_post_type( "podcasts", $args );

// End of CPT_register_my_cpts_podcasts()
}

Also published on Medium.

Surf the Dream is a blog that has been running since the mid 2000's when it started on BlogSpot. Over the years it's been rebranded as justinavery.me (now my resume) and JaveryDesign.com (which now redirects back to this site).

I offer consultation services through Simple Things, produce a range of educational pocket notebooks, write about the Universe and run a responsive design and a RWD Weekly Newsletter