I have multiple stage process that integrates complex faceting into WP queries. Problem is – the deeper I get the more fuzzy I become on how it is supposed to work and I could use a guideline rather than being inventive (and digging myself a hole).

General stages I have (pseudo-code, but close to real):

  1. retrieve value from URL (something simple like person=1 )
  2. process value and append custom query instructions ( $relationship_query[] = array('relationship' => 'person_to_cat', 'object' => 1 ); )
  3. retrieve final custom query instructions and generate appropriate SQL directives

By now I have a lot of such going on (relationships, taxonomies, dates) and it’s becoming fragile (one corner of code doesn’t put value where other corner of code expects it to find and everything comes apart).

So large question is – what is proper protocol to pass, receive, store and process custom data to WP via URL?

Smaller parts:

  • how to ensure I don’t collide with WP internals?
  • where and how I store intermediary data?
    • difference between query and query_vars properties? They seem to be used almost same in code, yet they tend to hold different values and methods favor query_vars one.

3

I think the WP function you are looking for is add_rewrite_tag. It aims to add custom GET params to your URL and include it automatically in query_vars.

For example, you can add the following to the init hook :

add_rewrite_tag('%person%','([^&]+)');

For a url like http://example.com?person=joe, the global $wp_query will have

$wp_query->query_vars['person'] = 'joe'

You can also add a rewrite rule to make the URL prettier, for example http://example.com/person/joe

add_rewrite_rule('^person/([^/]*)/?','index.php?person=$matches[1]','top');

See the Rewrite API for more information and examples.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *