-1

I am creating a custom post for example universities

Now while creating universities I will suppose I will add a University having the name RMIT in that post I will add a custom meta key country AU(Australia)

I want to access this single university's details using URL www.mywebsite.com/au/rmit

and want to get a list of all universities of Australia using RUL www.mywebsite.com/au/

this will list all universities in Australia

So I am adding the country name in the university post as a meta key let me know how can generate this type of URL you can see in this image slug that I want meta key for country

because I am not able to generate dynamic URL for post based on meta key values.

10
  • the permalink that you can see in the given screenshot I am achieving that right now with the help of the following code /* get counrty meta value */ $counrty = get_post_meta( $_GET['post'], '_country', true ); if($counrty != ''){ $slug = strtolower($counrty); }else{ $slug = 'universities'; } Mar 15 at 15:21
  • this would be much easier, and significantly faster if you had a country taxonomy. Putting au/ in the slug, or trying to insert it from post meta into the URL will be expensive and difficult, but there are far easier ways to get that URL. In future you should ask how to put au in the URL and mention what you tried, rather than asking how to fix what you tried ( X Y Problem ), we can only answer the question you asked, not the problem you have.
    – Tom J Nowell
    Mar 15 at 16:00
  • @TomJNowell thank you for the update but I think using AU as taxonomy will be static I want to make it dynamic because it's not just AU it may be for other counties like the US, UK, CA etc. I will be adding different universities and associating with different countries and later I would like to access them base on country as I have given example of AU Mar 15 at 16:06
  • what do you mean by static? That doesn't make sense, if you pick Australia in your field the URL would be au not us, I don't see how it would be different. If you want the URL to determine the meta field so it works backwards then that won't work either as you'll be unable to use any caching, and most importantly you'll have different pieces of content for the same post. WP will redirect it to the canonical URL and you'll face SEO penalties, as well as sitemaps that don't make sense
    – Tom J Nowell
    Mar 15 at 16:45
  • if you want different URLs for different countries then you need different posts, e.g. au/rmi us/rmi and uk/rmi would be 3 posts, not 1. The you'd have to build archives for each university from scratch. A taxonomy already give you archives, templates, and listing, as well as the URLs. If the goal is to have a single university post reachable by all its country codes then you'll have duplicate content penalties, but you'll also face the problem that your metabox only lets you pick a single country
    – Tom J Nowell
    Mar 15 at 16:46

0

Your Answer

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

Browse other questions tagged or ask your own question.