• Resolved amandathewebdev

    (@amandathewebdev)


    Hello there,

    Great plugin! I love it. I ran into a need though and I’m not sure this plugin can do what I need. I am setting up CPTs for different services in different counties in PA. What I would like to do is have one option in the admin menu for the CPT named: County Pages. When someone creates a County Post, they should be able to choose a county, and from there a service type.

    So it should go: County Page > Bucks County, PA > Service Type

    And the URL should be: bucks-county-pa/service-type/post-title

    I’m not sure if this is something I can do with taxonomies? I’m not even sure what the language is here to ask or Google the question properly. I created a template (single-county-job.php) and it should apply to each county under the County Page CPT. At first I thought I could achieve this with categories but then the URL wouldn’t be the desired path.

    Any input would be appreciated!

    https://wordpress.org/plugins/custom-post-type-ui/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Will answer tomorrow when back from a long weekend. Thank you for your patience.

    Thread Starter amandathewebdev

    (@amandathewebdev)

    Thanks for the heads up, tomorrow is good 🙂 Just a little more insight for when you get back:

    I guess my issue really boils down to a permalink one. I can’t get rid of the CPT name from the URL, even when adjusting my permalinks. Ideally if I could get rid of that from the URL it would be perfect.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Removing the post type slug, like the way I believe you need, is not something we have built into our plugin itself. We try to focus just on the registration of the post types/taxonomies and stay hands off for afterwards.

    That said, I think this is what you are needing, or at least would put you on a more proper path forward. http://www.markwarddesign.com/2014/02/remove-custom-post-type-slug-permalink/

    Thread Starter amandathewebdev

    (@amandathewebdev)

    Hi Michael,

    Thank you so much for getting back to me and offering support. The plugin is great and your quick responses are awesome!

    I did give the code from the link you provided a try and it worked, but then I got a 404. I ran into this issue yesterday, and there are redirect rules I could set up to deal with the 404 but I was reading that the redirects are a hack and not good to have in this case.

    Maybe I need to deepen my understanding of the CPT. What do you think I could do to have /county-state/post-title without having to make a CPT for each county? Hypothetically if I cover all 50 states eventually, with a CPT for each, that would crowd the admin menu. And then I have to make the same template for each state, replicate the custom fields for each state. But my slugs would be correct.

    Basically the main concern with the slugs is SEO – if it is /county/county-state/post-title Google would crawl the site and think there is a /county/ parent page/archive but there wouldn’t be, wouldn’t this throw off the sitemap? It also wouldn’t make sense from a users standpoint to add another layer in there.

    Thanks again for bearing with me here. I really appreciate the help! For real!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Just to be certain, did you flush you rewrite rules after you applied the code in the link above? You can do that simply by visiting the permalinks page.

    You could make the post type hierarchical, allowing you to set parents. It’d provide only 1 admin menu item and allow for nesting when viewing the actual posts.

    Thread Starter amandathewebdev

    (@amandathewebdev)

    Yes I did flush the permalinks, and reset my .htaccess to what it was before I started messing with getting rid of slugs.

    It it possible I just struck a limitation with the way WordPress handles CPTs? Do you think getting rid of the slug then writing a rule to redirect is bad? Or is it the only viable solution? I am determined to solve this! haha

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I personally have no issue with amending the rewrite rules like the link does, and at least when it’s working like it theoretically should, it does get the job done. Regardless of who thinks it’s X amount of a hack.

    Thread Starter amandathewebdev

    (@amandathewebdev)

    Thank you, that is comforting. I’m trying out that route.

    Thread Starter amandathewebdev

    (@amandathewebdev)

    Alrighty, thanks to Michael and a fellow named Vard on Stack Overflow I was able to get this resolved. For anyone looking at this in a similar jam, this is the code I used, and here is the link to Vard’s help: http://stackoverflow.com/questions/32100011/custom-post-types-slug-and-permalinks-issue-seo-and-structure/32100586?noredirect=1#comment52141071_32100586

    //Rewrite cpt slug to get rid of 404 generated by the function below
    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);
    }
    
    //No longer use links with /county-job/ as prefix and target anywhere this link might generate with filters.
    add_filter( 'post_link', 'my_post_link', 10, 3);
    add_filter( 'page_link', 'my_post_link', 10, 3);
    add_filter( 'post_type_link', 'my_post_link', 10, 3);
    add_filter( 'category_link', 'my_post_link', 11, 3);
    add_filter( 'tag_link', 'my_post_link', 10, 3);
    add_filter( 'author_link', 'my_post_link', 11, 3);
    add_filter( 'day_link', 'my_post_link', 11, 3);
    add_filter( 'month_link', 'my_post_link', 11, 3);
    add_filter( 'year_link', 'my_post_link', 11, 3);
    add_filter('post_link', 'my_post_link', 1, 3);
    function my_post_link($post_link, $id = 0) {
      $post = get_post($id);
      if(is_object($post) && $post->post_type == 'county-job') {
          return str_replace('county-job/', '', $post_link);
      }
      return $post_link;
    
    }
Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Grouping posts in admin under a category name’ is closed to new replies.