WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a very peculiar requirement, hopefully I can explain it without being too confusing. I created a page template where I list some properties I get from an external XML file, so far no problems, let's say the URL is like this: http://www.mysite.com/properties/

Each property has a link that should redirect the user to a "Single Property" page that displays more info about it, what I was wondering is if there's a way to make the link like this http://www.mysite.com/properties/123 ? Where "123" would be the id of the property, basically if I have the url like properties/some_id I want to be able to load a view file (like the single.php or page.php files) but specific to this url condition.

Is this possible? Thanks in advance!

share|improve this question
up vote 32 down vote accepted

Add this to your theme's functions.php, or put it in a plugin.

add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
        'top' );

add_filter( 'query_vars', 'wpse26388_query_vars' );
function wpse26388_query_vars( $query_vars ){
    $query_vars[] = 'property_id';
    return $query_vars;

This adds a rewrite rule which directs requests to /properties/ with any combination of numbers following to pagename properties, with the query var property_id set. Just be sure to visit your permalinks settings page and save to flush rewrite rules, so this new rule will be included.

In your page-properties.php template, get_query_var('property_id') will return the property id if it was set, if it's not then show the default properties page.

share|improve this answer
This was CLOSE to working for me but I needed to add: add_filter('init','flushRules'); function flushRules(){ global $wp_rewrite; $wp_rewrite->flush_rules(); } – tooshel Nov 13 '12 at 17:52
@tooshel you definitely don't want to flush rules on every request, it is an expensive operation and will slow your site to a crawl. you only need to flush rules once, on plugin activation, or just by visiting the permalinks settings page. – Milo Nov 13 '12 at 20:17
Yeah, I get that . . . but when you are testing it's nice that it's in there! – tooshel Nov 14 '12 at 20:32
@Milo, may I request you to have a look at a question on a different topic regarding wordpress custom menus at wordpress.stackexchange.com/questions/116892/… ? – Istiaque Ahmed Oct 6 '13 at 13:11
A smarter rewrite url regexp might be ^properties/([0-9]+)/?. Otherwise it would match something like example/properties/1 – Ryan Taylor Dec 12 '14 at 17:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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