1

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.

2

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

  • 1
    Hey @benny-ben, thanks for the answer, actually I gave it more time and the solution came to me. You answer should still work, just as the solution I did. Mine is different. The problem was that I was appending arrays on the 'tax_query' value, but not listing them with commas, so the value was passed just as "ArrayArray" and not as each arrays listed. What I did was build an array at each one, with a variable, and in the end, build the $args['tax_query'] like this: $args['tax_query'] = array($type, $status, $city); So it works now :D – atempel Sep 11 '18 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.