0

Is it possible to create a custom url for custom post types?

I have a directory website where in user can post or add a listing in many cities, I have a custom post type name ads. And all of the ads post titles are phone numbers. So when I create a new ads post in a city (e.g. Birmingham AL), the city will be save as a post meta post_ads_city,

Automatically the url will be mysite.com/ads/123-123-1234

Is it possible to make the url slug based on the city where the ads is posted? so it will be like this:

mysite.com/birmingham-al/123-123-1234 (if the postmeta "post_ads_city" = "Birmingham AL") mysite.com/las-vegas/123-123-1234 (if the postmeta "post_ads_city" = "Las Vegas")

  • you will save yourself lots of trouble by prefixing your structure with ads, so it it is mysite.com/ads/birmingham-al/123-123-1234. with your current desired structure, there's nothing to distinguish those requests from the page post type, all pages will 404. – Milo Dec 17 '14 at 16:32
  • i can do that, but the directory site i am doing is already live for 2 years now and it has been develop using codeigniter, so what im doing is recreating that site again into wordpress thats why i need to keep the urls the same for seo purposes – Ramje Dec 18 '14 at 15:21
0

Use add_rewrite_rule()

function custom_rewrite_rule() {
        add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','index.php?page_id=12&food=$matches[1]&variety=$matches[2]','top');
    }
    add_action('init', 'custom_rewrite_rule', 10, 0);

you can use if ( 'myposttype' == $post->post_type ){} to make it post type specific. Weather you want to add it inside the function or wrapping the add action is up to you.

  • can you show an example of using if ( 'myposttype' == $post->post_type ){} to make it post type specific? (this is sort of a trick question- it's not possible) – Milo Dec 17 '14 at 16:31
  • @Milo you would know better than me! I guess there are a bunch of ways to examine this, this post outlines it more: wordpress.org/support/topic/… – Rizzo Dec 17 '14 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.