• Hello, I am trying to create a locations post type where I can add parent categories and sub-categories, where the slug would reflect the complete hierarchy.

    For example, I want to have a locations page that lists all my locations, which would be parents categories – eg, Los Angeles, Chicago, New York. Then, each of those parent categories have sub-categories- eg, for LA, it would be Hollywood, Beverly Hills, Malibu. Then for each one of those, it would have the actual address post – eg, 1 Hollywood Blvd, 100 Sunset Ave, 500 Vine St. So the actual individual post would be 1 Hollywood Blvd, and the slug would be mysite.com/locations/los-angeles/hollywood/1-hollywood-blvd.

    And my locations page would show every parent location, each parent location page would show the sub-categories in there, and each sub-category page would show each address post.

    Is that possible and easy to do with custom post types?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes, it’s possible. How easy it is depends on how experienced you are with PHP and customizing WP. With adequate experience it’s sort of easy, but not dead simple. To get category URLs to work like that takes some custom rewrite rules.

    What would be even easier is to create the locations post type with all your intended categories as hierarchical post types instead of fussing with categories. Then the URLs you desire will happen automatically without the need to fuss with rewrite rules.

    For example, you have a location post named Los Angeles. It has several immediate children like Hollywood, Malibu, etc. Then posts like Hollywood, whose parent is Los Angeles, has immediate children like 1 Hollywood Blvd. You may assign what ever categories you want, but they would not affect the URLs generated. The URLs are generated only by the ancestors of any particular locations post.

    Thread Starter navyspitfire

    (@navyspitfire)

    Hi @bcworkz – I like the second idea better, thanks. So I just need to create the locations post type and all the hierarchical posts will contain the correct slug URL’s?

    My next question would be, if I do it that way, I will need to have each category/’parent’ post have a dedicated page that lists out all its children. Meaning, /locations/ would should Los Angeles, Chicago, New York, but not its children. Then /locations/los-angeles/ would have a list of its children, so Hollywood, Malibu, Beverly Hills. Is there someway to have this automatically happen? I don’t mind getting deep into code, it would be best to automatically update when new posts are created.

    So essentially the main functionality I am looking for is automatic archiving on all the pages.

    Moderator bcworkz

    (@bcworkz)

    Yup, hierarchical custom post types automatically generate URLs just like your example ?? It may make a difference in what arguments are used to register the post type, but the most logical choice will likely be the right one.

    Listing immediate children without grand-children is easily done with a custom WP_Query object by providing a “post_parent” argument set to the ID of the current locations post. I suggest you create a custom single post template named single-locations.php. It will be used for any single locations post request. It can output different content based on arbitrary criteria. For example, it generally will list all immediate children, but if the post has no children, as in say, 100 Sunset Ave, the template can instead display the post content and/or custom fields, etc.

    You can also define an archive template named archive-locations.php to cover the situation when example.com/locations/ is the request. It would essentially be the same WP_Query/post_parent query, except the post parent will be set to 0 (zero) so only top level location posts (cities in your example) will be listed.

    Thread Starter navyspitfire

    (@navyspitfire)

    Hmm I think I understand. So my locations page would have it’s own template (archive-locations.php) with a WP_Query, while all my sub-locations (LA, Chicago, New York) would be using the same template (single-locations.php)? And with each template I could show the immediate children?

    Moderator bcworkz

    (@bcworkz)

    Exactly!

    At a minimum, the query arguments would be something like:

    $args = array(
       'post_type'=>'locations',
       'post_parent'=> get_the_ID(), // use 0 on archive-locations.php
    );

    Other arguments may or may not be required. “posts_per_page” maybe? After the arguments, continue with one of the example loops shown in the WP_Query docs. Once you confirm your are getting the proper results, you can alter the output to suit. For example, for the no (child) posts found condition, after calling wp_reset_postdata(), output the current post’s title and content like one would do on a normal single post template.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom post type with categories, looking for specific slugs’ is closed to new replies.