Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

up vote 16 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(){
    add_rewrite_rule(
        'properties/([0-9]+)/?$',
        'index.php?pagename=properties&property_id=$matches[1]',
        '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
3  
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
7  
@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 at 17:34

Your Answer

 
discard

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.