Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Something similar to what you want, but different:

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

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

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

    Put this in your theme’s functions.php: https://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.

    Thread Starter the_lar

    (@the_lar)

    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

    Moderator keesiemeijer

    (@keesiemeijer)

    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:

    https://codex.www.ads-software.com/Function_Reference/wp_insert_post
    https://codex.www.ads-software.com/Function_Reference/wp_insert_category
    https://codex.www.ads-software.com/Function_Reference/wp_create_category

    Thread Starter the_lar

    (@the_lar)

    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

    Moderator keesiemeijer

    (@keesiemeijer)

    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.

    Thread Starter the_lar

    (@the_lar)

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

    Kevin

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Customising permalink handling’ is closed to new replies.