So, what I’m trying to do, is to make an ajax filter, this is the portion of the filter code that builds the $args based on the data that came from the filter form, and afterwards (not in this code) it gives the results of the query.
I have 3 custom taxonomies (and their functionality from form):
– type_emp (one option comes from form)
– status_emp (ONE OR MORE options comes from form)
– city_emp (one option comes from form)
Thing is: I’m not able to use them in the WP_Query as filters, since they will match the post even if it doesn’t have all the terms.
Example
type_emp is 1
status_emp is null
city_emp is 2
It will return posts:
(type_emp / status_emp / city_emp)
post 1 ( 1 / 2 / 3)
post 2 ( 2 / 1, 4 / 2)
post 3 ( 1 / 4 / 2)
As you can see, post 3 should be the only one returned, since he has all the parameters. Is there a way to restrict the query like that?
Also, I need to match status_emp more than once, with sometimes multiple terms, down in the code, I explode the string to make an array and put it as terms in a tax query. How to I match posts that have at least one of them?
$args = array( 'orderby' => 'date', 'order' => 'DESC', 'post_status' => 'publish' ); if ( isset( $_POST['typefilter'] ) && $_POST['typefilter'] !== '' ) { $args['post_type'] = 'post_type_x'; $args['tax_query'] = array( array( 'taxonomy' => 'type_emp', 'field' => 'id', 'terms' => $_POST['typefilter'] ) ); } if ( $_POST['typefilter'] == '') { $args['post_type'] = 'post_type_x'; } // If two or more options are selected they are sorted below $query = explode('&', file_get_contents("php://input")); $params = array(); foreach( $query as $param ) { list($name, $value) = explode('=', $param, 2); $params[urldecode($name)][] = urldecode($value); } if( isset( $params['statusfilter'] ) && $_POST['statusfilter'] !== '' ) { $args['tax_query'] .= array( array( 'taxonomy' => 'status_emp', 'field' => 'id', 'terms' => $params['statusfilter'] ) ); } // for taxonomies / categories if( isset( $_POST['cityfilter'] ) && $_POST['cityfilter'] !== '') { $args['tax_query'] .= array( array( 'taxonomy' => 'city_emp', 'field' => 'id', 'terms' => $_POST['cityfilter'] ) ); } $query = new WP_Query($args);
Sorry if my question is a bit confuse, but I tried to be the most clear possible.
1 Answer
Try this way:
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
if ( isset( $_POST['typefilter'] ) && $_POST['typefilter'] !== '' ) {
$args['post_type'] = 'post_type_x';
$args['tax_query'][0]['taxonomy'] = 'type_emp';
$args['tax_query'][0]['field'] = 'id';
$args['tax_query'][0]['terms'] = $_POST['typefilter'];
$args['tax_query'][0]['operator'] = 'IN';
}
if ( $_POST['typefilter'] == '') {
$args['post_type'] = 'post_type_x';
}
// If two or more options are selected they are sorted below
$query = explode('&', file_get_contents("php://input"));
$params = array();
foreach( $query as $param )
{
list($name, $value) = explode('=', $param, 2);
$params[urldecode($name)][] = urldecode($value);
}
if( isset( $params['statusfilter'] ) && $_POST['statusfilter'] !== '' ) {
$args['tax_query'][1]['taxonomy'] = 'status_emp';
$args['tax_query'][1]['field'] = 'id';
$args['tax_query'][1]['terms'] = $params['statusfilter'];
$args['tax_query'][1]['operator'] = 'IN';
}
// for taxonomies / categories
if( isset( $_POST['cityfilter'] ) && $_POST['cityfilter'] !== '') {
$args['tax_query'][2]['taxonomy'] = 'city_emp';
$args['tax_query'][2]['field'] = 'id';
$args['tax_query'][2]['terms'] = $_POST['cityfilter'];
$args['tax_query'][2]['operator'] = 'IN';
}
$query = new WP_Query($args);
I haven’t tested it but theoretically it should work :), tell me if it works or if you need more help