creating real time tax_query in WordPress

Several time we need to create real time tax_query in WordPress. The case can be

  1. some interactive form for users in WordPress
  2. some complex searches in WordPress
  3. 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,

//Search arguments
$searchargumentarray = array(
	'post_type' => 'yourposttypename',
	'numberposts' => - 1,
	'meta_query' => array(
			'key' => 'post_meta_data1',
			'value' => $somevalue_min,
			'compare' => '>='
		) ,
			'key' => 'post_meta_data2',
			'value' => $somevalue_max,
			'compare' => '<='
		) ,
	) ,

//pushing the created array with the key tax_query
$searchargumentarray['tax_query'] = array(
	'relation' => 'AND',
		'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

  1. You must have added the array input fields in the form that submit the field
  2. We took the data that is submitted by form using post method.
  3. Than we have declare a blank array
  4. After that we pushed the condition we want to put. This can be AND/OR.
  5. After this we ran a for loop to create the real time query, and keep pushing the data to the array
  6. Once we are done with fields we declare the arguments for get_posts() method.
  7. And we again pushed the array with desired key value. In our case that is tax_query().
  8. We are done with this.

About Pashupatinath Mishra

Pashupatinath Mishra is Software Developer and our Fulltime blogger. He is having good knowledge on the Different Technologies and also having shareable knowledge on Nutrition, Science Topics, Travel and History.


Article Name
Real time tax_query in WordPress
This article is about real time array argument generation for searching data in WordPress. here you can have information about real time generation of the query using tax_query and meta_query.
Publisher Name
Pashuaptinath Mishra

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.