WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Customising permalink handling (7 posts)

  1. the_lar
    Member
    Posted 1 year ago #

    Hi all,

    I have a project on to build a real estate search engine and I need to modify the way WordPress handles the permalinks.

    This is what I need to achieve:

    If the user comes into the site on the URL http://www.mysite.com/state/town/for-sale/ this would be interpretted as: http://www.mysite.com/state/town/?type=for-sale

    If the user comes into the site on URL http://www.mysite.com/state/town/for-rent/ this would be interpreted as: http://www.mysite.com/state/town/?type=for-sale

    Finally if the user comes into the site on URL http://www.mysite.com/state/town/property/3781/ this would be interpreted as: http://www.mysite.com/state/town/?type=property&id=3781

    As you can see, all three requests result in the same physical wordpress page - /state/town/ but the page template will behave according to the search query.

    Can anyone help with the rewrite rules I'd need in place to facilitate such a structure?

    Many thanks
    Kevin

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Something similar to what you want, but different:

    These urls are a custom taxonomy
    http://www.mysite.com/properties/state/town/for-sale/
    http://www.mysite.com/properties/state/town/for-rent/

    You could even do this:
    http://www.mysite.com/properties/state/for-sale/
    http://www.mysite.com/properties/state/for-rent/

    single posts urls are a custom post type
    http://www.mysite.com/property/post-title

    Put this in your theme's functions.php: http://pastebin.com/G8YUGex9

    It creates a custom post type "property" and a custom taxonomy "location" and adds the rewrite rules for "for-sale" and "for rent".

    It also adds a query_var "type". On a taxonomy template you can get the type like so:

    $type = get_query_var('type');
    if( 'for-sale' == $type ) {
    // do stuff (alter query) for-sale
    }

    You probably have to re-save your permalink structure in wp-admin > Settings > Permalinks for this to work.

  3. the_lar
    Member
    Posted 1 year ago #

    Hi keesiemeijer,

    Really appreciate you taking the time to help me out!

    It's a really great starting point, the main issue I have though is that I didn't intend having a separate post in wordpress for each property. Individual agents will be uploading data and I have a complicated script which parses this into separate tables. I suppose it would be possible as part of this script to create a post for each property (there will be thousands) but I'm not too sure how to go about doing that???

    As I say, thanks for your help again

    Kevin

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    As I don't know the script and how you query the database there is not much I can do to help.

    Maybe you could convert the data to posts:

    http://codex.wordpress.org/Function_Reference/wp_insert_post
    http://codex.wordpress.org/Function_Reference/wp_insert_category
    http://codex.wordpress.org/Function_Reference/wp_create_category

  5. the_lar
    Member
    Posted 1 year ago #

    Just as an update on this, I have set up a single 'dummy' post in Properties and I am rewriting all queries to /property/somepropid/ to 'index.php?property=dummy' and reading the actual id into a variable - here's my addition to keesiemejers code:

    $options = array(
      'rules'=>array(
        ...,
        'property/(.+?)/?$' => 'index.php?property=dummy&propid=$matches[1]'
      ),
      ...
    );

    This seems to do the trick and means that I can handle all /property/ requests without there needing to be an actual post for it.

    I do have one question - I have put keesiemeijer's class into my functions.php file, but I've read that it's not particularly efficient to keep flushing_rules - would I be better writing this class into a plugin?

    Thanks
    Kevin

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    but I've read that it's not particularly efficient to keep flushing_rules

    That's true but the code I gave you only flushes the rules once (if there are new rewrite rules). It doesn't flush the rules on subsequent pageloads. This is especially helpful if you are testing new rules. Otherwhise you'll need to re-save your permalinks every time you add a new rule.

    If you're done with testing you can comment out this line if you want:

    // add_action('wp_head', array(&$this, 'flush_rules'));

    would I be better writing this class into a plugin?

    Yes. That way you keep all your code together and can incorporate the functionality in another theme if you ever wanted to switch.

  7. the_lar
    Member
    Posted 1 year ago #

    Ah, I see - I thought it flushed on every page refresh. Thanks again for your help - really appreciate it.

    Kevin

Topic Closed

This topic has been closed to new replies.

About this Topic