Support » Developing with WordPress » How to change URL structure for custom post list?

  • This is actually pretty straightforward, but I hardly know how to best express what I’m trying to do.

    I’m building a site for a company that owns planned communities; they want listings of their homes for sale. To that end, I created a custom post type of “Listings.” Categories like “Alphatown” and “Betatown” serve to segregate the listings within custom loops of a single category each. Those loops are put on a page with a custom wp_query, so the URL for the listings pages would be something like this:

    company.com/alphatown/homes/

    The problem is that as soon as you click on one of the individual listings, the URL becomes something like this:

    company.com/listings/listing-1

    whereas it would be preferable if it were this:

    company.com/alphatown/homes/listing-1

    Is there any way in WordPress to accomplish this? Or will I have to make “Alphatown” and “Betatown” totally separate WordPress installs in order to make the URLs conform to this structure?

    • This topic was modified 3 years, 8 months ago by Who_Dat.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    No don’t make more installs, there’s ways to achieve most things with custom coding. This requires a two part solution, one to generate the proper links, the other telling WP how to interpret them. The first part depends on where the links are generated. Typically one of your theme templates. If they come from a WP function, there’s likely a filter hook you can use. Or you could replace whatever is used on the template with your own custom function that builds the link based on the posts’s categories.

    Telling WP how to interpret such links is done with add_rewrite_rule(). The important thing here is to develop a regexp that matches to the links you need it to match, but not other valid WP links that should not be processed by this rule. For example, if “homes” is always going to be the second link term and no other WP URL has this characteristic, then including “homes” in the regexp will work perfectly. Review the docs on how to get this function to work for your needs.

Viewing 1 replies (of 1 total)
  • The topic ‘How to change URL structure for custom post list?’ is closed to new replies.