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()
}
I realize this post is over five years old, but thank you! I’ve googled for at least an hour and this was what I needed.
Wahoo, glad you got it working!