wp query - Not able to get my custom search result using meta_query and tax_query together?

admin2025-01-07  4

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 );

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 );
Share Improve this question edited Jul 18, 2017 at 7:49 Max Yudin 6,3782 gold badges26 silver badges36 bronze badges asked Jul 17, 2017 at 14:06 Sandeep MohantySandeep Mohanty 1 1
  • When you need help, next time, please, make your code readable by anybody. – Max Yudin Commented Jul 18, 2017 at 7:52
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736255200a263.html

最新回复(0)