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 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.
share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

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.

share|improve this answer
2  
I found some time (at last) to read through all relevant code in WP and WP_Query and while there are a lot of things happening there, I think that add_rewrite_tag() seems most succinct way to make WP aware of custom query var and play well with it. –  Rarst Oct 18 '12 at 15:05
add comment
  1. Check out wp-includes/class-wp.php to check any reserved query_vars
  2. I'm not sure what you mean by intermediary data. Can you explain more?
  3. WP load the page based on a query_string on URL which is translate as query, they are then process by WP_Query to determine which query_var is being used.

Example: by loading a page with /?pagename=about, WP will process the query string and find the query_var being used is page_id.

Another example: /?cat=1, /?category_name=uncategorized and /category/uncategorized have same query_var 'cat'

share|improve this answer
add comment

I would have assumed it be done via $_GET and/or $_POST, then manipulate accordingly.

Alternately, store the massive bunch of data in the db then parse an id to the url like so

$data_id = $_GET['id'];

$data = $wpbd->get_results("SELECT * FROM wp_epic_table WHERE id=".$id);

The query obviously won't be that simple, it may involve a lot of joins and join tables. That's what I do in one of my pages, parse a unique id then build the data via MySQL.

share|improve this answer
add comment

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.