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

  • Examples
  • Podcasts
  • Resources
  • News
  • Articles

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

  • It’s held in the database and not against your theme, so it’s transferable between different themes more easily
  • The Plugin requires no coding knowledge what so ever, so barrier to entry is quite low

Cons

  • Requires an additional trip to the database so is harmful to performance (although I don’t know if we’re talking 10’s, 100’s, or 1000’s of a second
  • If the plugin author stops support you might run into trouble with WordPress Upgrades.

funtions.php

Pros

  • Using Native WordPress functionality so backwards compatibility guaranteed over time.
  • No additional plugins or trips to the DB, so a faster overall experience (both front and backend)

Cons

  • Requires access to your functions.php file and some coding knowledge
  • If you’re switching themes you will also need to move that part of the functions.php file across.

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

  • Examples : /examples/example-post
  • Podcasts : /podcasts/podcast-post
  • Resources : /resources/resource-post
  • News : /news/year/month/example-post
  • Articles : /articles/article-post

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

  • Examples : /news/examples/example-post
  • Podcasts : /news/podcasts/podcast-post
  • Resources : /news/resources/resource-post
  • News : /news/2016/09/rwd-relaunch
  • Articles : /news/articles/article-post

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()
}

View Permanent Page

More articles...

  1. Time tracking with Toggl

    The pie chart for this XKCD Comic is me all over, and I often feel as though I’m turning into the procrastination monkey Time tracking is one of those things that I just hate doing for a few different reasons. I spend more time trying to remember how I spent my time then doing something […]

    Read more

  2. Progressive Invision – Offline use

    On the way home yesterday afternoon I was going through a couple of designs that had been uploaded onto Invision with the Account Manager on the project. “On the way home” entails a 40 minute train ride for which I fortunately have access to WIFI although it is pretty patchy from time to time. I […]

    Read more

  3. SpaceX Notebooks — Phobos & Deimos

    As you may be aware I started a small Notebook business recently, Back Pocket Notebooks, with a set of responsive design notebooks for my responsively designed audience. This week I received the next batch of notebooks which I have added to the stock list — a set of SpaceX Mars Posters. Over the next couple […]

    Read more

  4. Google Search Console Updates – Content Keywords & Site Grouping

    If you manage a website the chances are that you’ve heard of and use the Google Search Console, the artist formally known as Webmaster Tools. If you’re sitting there scratching your head and wondering what I’m talking about then go and check out this article from Moz and get yourself started. The console is always […]

    Read more

  5. Getting back to podcasting

    You should be very aware that I love a great weekly newsletter. One of my recent favourite newsletters to receive is Paul Jarvis’ Sunday Dispatches where Paul imparts a very useful piece of nerdy knowledge each week. The content is around 95% educational and allows you to learn a lot about the web and how […]

    Read more

  6. Is CriticalCSS harming our site performance?

    In a recent article on Medium, JP DeVries wrote about “Unfolding Critical CSS“. Are you building a single web page that users will visit once never to return to or are you building a multi–page website that most users click around on for a while? If you inline an average of ~20kB of critical CSS […]

    Read more

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