What is appropriate flow for custom data from URL in WP_Query?

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):

Read More
  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.

Related posts

Leave a Reply

3 comments

  1. 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.

    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’

  2. 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.