0

I created a custom search where i am able to show the Fields (ACF fields) and also Taxonomies. But while I am searching by taking the combination of both Taxonomies and meta fields, i am not getting the results. It is giving the results if i will search either Taxonomies or meta fields. How to make both conditions work together?

This is my code where i mentioned both meta_query and tax_query:-

<?php
if($_POST){
    $_speciality = $_POST['speciality'] != '' ? $_POST['speciality'] : '';
    $_students_last_name = $_POST['students_last_name'] != '' ? $_POST['students_last_name'] : '';

    if($_speciality!=" "){
        $param1 = array(
            'key' => 'speciality',
            'compare' => '=',
            'value' => $_speciality
        );
    } else{
        $param1 = "";
    }

    if($_students_last_name!=" "){
        $param2=array(
            'key'       => 'students_last_name',
            'compare'   => 'LIKE',
            'value'     => $_students_last_name
        );
    } else{
        $param2="";
    }

    $args = array(
        'post_type' => 'students',
        'orderby'   => 'date',
        'order'     => 'DESC',
        'tax_query' => array(
            array(
                'taxonomy'  => 'Music',
                'field'     => 'slug',
                'terms'     => $param1
            )
        ),
        'meta_query' => array(
            'relation' => 'AND',
            $param2
        ),      
    );

} else {
    $args = array(
        'post_type' => 'students',
        'orderby' => 'date',
        'order' => 'DESC'
    );
}

$the_query = new WP_Query( $args );
1
  • When you need help, next time, please, make your code readable by anybody.
    – Max Yudin
    Jul 18, 2017 at 7:52

1 Answer 1

0

Looks like a common problem: meta_query & tax_query combined = fail. Look, i've the same problem and i mentioned two links (possible solutions for you) in my question (Combine tax_query and meta_query in WP_Query). Can you use/handle something of this? If yes, maybe it's your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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