Several time we need to create real time tax_query in WordPress. The case can be
- some interactive form for users in WordPress
- some complex searches in WordPress
- search with multiple dynamic fields in WordPress
In this case we need to populate the real time data basis on the user selection. We have several options to use to fetch the required data. Here we can use wp_query for that purpose but that is not ideal way even that is complex one. Another way to fetch the desired data is that we can use the simple sql query. But again that is not advisable as we can simply manage this using the WordPress core functionality. that is its arguments to get_posts() method.
How can we use get_posts() for custom query
To get the results using the get_posts() method we can simply use the meta_query() for Post meta data and the tax_query() for the data that is saved in the taxonomy. so lets see how can we fetch the real time data using the dynamic query generation.
For this we need to understand the simple core php concept of the array(). you might know that array is a special variable or a object, that holds more than one value at a time. here we need to work on multi dimensional array. so our logic is to push the data in the argument array() real time this can be achieved using the array_push() method of array.
int array_push ( array &$array , mixed $value1 [, mixed $... ] )
so we just need to use this code to push the dynamically created data lets see how this will be done.
$searchdata_type = $_POST['submits_array_field']; $typesearcharray = array(); if (count($property_type) > 1): // define the condition to be shown $typesearcharray = array( 'relation' => 'OR', ); //For loop to create the realtime data foreach($searchdata_type as $sintype): array_push($typesearcharray, array( 'taxonomy' => 'your_taxonomyname', 'field' => 'id', 'terms' => $sintype, 'include_children' => true, )); endforeach; endif; //Search arguments $searchargumentarray = array( 'post_type' => 'yourposttypename', 'numberposts' => - 1, 'meta_query' => array( array( 'key' => 'post_meta_data1', 'value' => $somevalue_min, 'compare' => '>=' ) , array( 'key' => 'post_meta_data2', 'value' => $somevalue_max, 'compare' => '<=' ) , ) , ); //pushing the created array with the key tax_query $searchargumentarray['tax_query'] = array( 'relation' => 'AND', $typesearcharray, array( 'taxonomy' => 'your_another_taxonomyname', 'field' => 'id', 'terms' => $somevalue_area, 'include_children' => false ) , ); $resultList = get_posts($searchargumentarray);
The above code is fetching the combination of the data that is of meta_query() and the tax_query() this is the way we can generate the dynamic search and real time query using the array_push() method.
Here we have fetched the post variable that is submitted by the form using the post method.
Steps to create real time tax_query in wordpress
- You must have added the array input fields in the form that submit the field
- We took the data that is submitted by form using post method.
- Than we have declare a blank array
- After that we pushed the condition we want to put. This can be AND/OR.
- After this we ran a for loop to create the real time query, and keep pushing the data to the array
- Once we are done with fields we declare the arguments for get_posts() method.
- And we again pushed the array with desired key value. In our case that is tax_query().
- We are done with this.