How to, troubleshooting, Tutorials, WordPress

Having Blog in your Post URL without affecting Custom Post Types

Is it possible to include “blog” in the Post URL?

I was working on a project where I was transferring to a different CMS (WordPress), and the original blogs all had “blog” in the URL. I wanted the same format in the new site, figuring this was the best thing to do for existing SEO.

However, the new WordPress site is also using Custom Post Types (CPTs) and I didn’t want to affect the URLs of those posts. My instinct was to change the Permalink Settings to add “/blog” to the front, but this also affected the CPT URL.

example of Permalink Structure with blog at the front

Checking with my good friend Google seemed to take me longer to find the solution than expected, and admittedly I may be just stupid, but I thought I’d write this to see if it helps anyone else in the same, or similar, situation.

Why not use a Custom Post Type plugin to handle this?

It is highly likely that a decent Custom Post Type plugin would be able to handle this “out of the box”, however I had already created the post type somewhat manually, using what I think is a very nifty little plugin called WPCode which typically means for me less plugins installed overall (seriously, check it out, it’s great).

Ultimately it meant I had basically entered the CPT code manually, but obviously had something missing that might allow me to “strip” or ideally “ignore” this starting part of the URL structure.

Links high up the Google SERP had me looking at various Custom Post Type plugin solutions, or more like stripping it after the post was published or upon retrieval which seems like a slow way of doing things (or if not slow, less efficient), meaning doing something via the functions.php file.

Finding what I thought was going to be my answer, as it at least spoke about the Post Type registering process, only gave me half the answer and contained my bug bear (one of many) of typically developers (yes I am looking at mostly ALL of you developers out there!) that tend to post incomplete code snippets under the assumption that I am smart enough to figure out the rest. Well I am NOT, dammit!

My answer with a more or less complete code snippet

My Custom Post Type is Projects. I want the URL / slug to appear as “project”, so something in the line of https://mysite.com/project/project-post-title.

So in my arguments array, the start of which looks like below:

// Set other options for Custom Post Type
      
    $args = array(
        'label'               => __( 'Projects', 'responsive-mobile-child' ),

I had the following right at the end or the array:

'rewrite' => array( 'slug' => 'project'), // my custom slug

    );

Which worked perfectly fine until I changed the Permalink structure as per above, which meant I now had https://mysite.com/blog/project/project-post-title.

Not cool, and probably not ideal for SEO considering the original “projects” had the more desirable URL.

All I had to do though was slightly edit that rewrite line, so it now included ‘with_front’ => False and now it looks like this:

'rewrite' => array( 'slug' => 'project', 'with_front' => False), // my custom slug
);

Saving the Permalink again at this point seems a good idea, so after doing that my “Projects” all had the appropriate URL (with just “projects” in the URL) and my regular posts have “blog” in the URL.

Huzzah!

Hope this has helped someone, and if any more explanation, or code snippets, are required then feel free to get in touch. And don’t forget to share!

You might also like:

Leave a Reply

Your email address will not be published. Required fields are marked *