phpwordpressurlcustom-post-typepermalinks

Custom post Types slug and permalinks issue - SEO and Structure


I see this issue has been brought up before but they don't seem to address my specific issue. I do feel my specific issue is a broad one though. That may seem paradoxical but hear me out.

For a site I'm working on in WordPress, I created some pages for the counties they serve. So far I have one for Bucks, Montgomery, and Delaware Counties, but there will be plenty more. From there we want people to be able to navigate to "Recent Jobs" based on the county that the client can add themselves. So it can be 1 to however many jobs the client wants to add. These pages will talk about a specific job in a specific town, then link to "Related Jobs" by job type, not county. So these go 3 levels deep, County Hub page > Specific job in specific town > Specific job type (ie: Bucks County, PA > Window Replacement Job in Newtown, PA > Window Replacement Job in Wherever).

Hope that makes sense. So what I did was create a Page for each County. Then I created a Custom Post type: County Jobs. I created custom taxonomies for each county to group them by county. I will probably need taxonomies to group them by job type also but I'm not there yet. I set up my template "single-county-job.php" and my Custom Fields to display only on this post type. The problem came when "/county-job/", the post type slug, displays in the URL. Adding that extra level is bad for SEO rankings. If you so a search for anything in Google you will see the first pages of results show sites that go only two levels deep: "URL.com/level1/level2" and anything with "/level3/" is at best on the third page (besides sites like Amazon or eBay). I ideally want the URL to be: "/bucks-county-pa/post-title" not "/county-job/bucks-county-pa/post-title". It also doesn't make sense for a user to go from the "/bucks-county/" page to "/county-job/bucks-county-pa/post-title".

So I found multiple ways to get the slug out of the URL but all result in a 404. Basically WordPress no longer recognizes the post as a post. There are some redirect rules I can add to get around the 404s but that is not good for SEO either. I also read it is bad practice, a hack, and shouldn't be done.

Have you set up similar pages/posts in the past and how did you handle it? That slug is a problem, but making a CPT for each county is not an option either because there are way too many and they would all be the same set up - it just doesn't make sense. Maybe this shouldn't be a CPT, but then how do I handle it? It doesn't seem like they should be pages, and how would I handle the Custom Fields?

I am stuck! Any help or insight you could provide would be a huge help.


Solution

  • Considering that the word county is static in your url, you can use it in order to make Wordpress recognize what you're trying to request. You can use generate_rewrite_rules filter in order to add your own rewrite rule:

    add_filter('generate_rewrite_rules', 'my_rewrite', 9);
    function my_rewrite($wp_rewrite)
    {
        $wp_rewrite->rules = array_merge(array(
            '^([^-]*)-county-([^/]*)/([^/]*)/?$' => 'index.php?post_type=county-job&county=$matches[1]-county-$matches[2]&county-job‌​=$matches[3]'
        ), $wp_rewrite->rules);
    }
    

    The second important part is to tell Worpdress to not use anymore links with /county-job/ as prefix. You can do this with the post_type_link filter:

    add_filter('post_type_link', 'my_post_link', 1, 3);
    add_filter('post_link', 'my_post_link', 10, 3);
    add_filter('page_link', 'my_post_link', 10, 3);
    function my_post_link($post_link, $id = 0) {
      $post = get_post($id);
      if(is_object($post) && $post->post_type == 'job') {
          return str_replace('county-job/', '', $post_link);
      }
      return $post_link;
    }
    

    Don't forget that you need to flush your rewrite rules in order to make this script working. You can do this by visiting the Permalinks section in WP back office.