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):
- retrieve value from URL (something simple like
person=1
) - process value and append custom query instructions (
$relationship_query[] = array('relationship' => 'person_to_cat', 'object' => 1 );
) - 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
andquery_vars
properties? They seem to be used almost same in code, yet they tend to hold different values and methods favorquery_vars
one.
- difference between
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 :
For a url like http://example.com?person=joe, the global
$wp_query
will haveYou can also add a rewrite rule to make the URL prettier, for example http://example.com/person/joe
See the Rewrite API for more information and examples.
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’
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
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.