查询2个分类法和元查询Wordpress

时间:2015-02-22 14:11:46

标签: php wordpress

我需要有关Wordpress查询的帮助。我有2个分类法,因此我使用了tax_query,而对于我使用meta_query的元帖子。我的问题是我希望通过搜索专业,保险或邮政编码来获取所有数据。

示例代码

$result_specialties = $_REQUEST['arr_spec'];
$result_insurance = $_REQUEST['arr_insur']; 
$zip_code = $_REQUEST['zip_code'];

//get term_id
$cat_spec = get_term_by( 'name' , $result_specialties, 'specialty' );
$get_spec_id =  $cat_spec->term_id;


$cat_insu = get_term_by( 'name' , $result_insurance, 'insurance' );
$get_insu_id =  $cat_insu->term_id;

//query 2 taxonomies
$args = array(
    'post_type' => 'sample_post',
    'relation' => 'OR', 
    'meta_query' => array(
        array(
            'key'     => 'zip_code',
            'value'   => $zip_code,
            'type'    => 'numeric',
            'compare' => '=',
        ),
    ),  
    'tax_query' => array (
    'relation' => 'OR',
        array(
            'taxonomy' => 'specialty',
            'field'    => 'id',
            'terms'    => array( $get_spec_id ),
        ),
        array(
            'taxonomy' => 'insurance',
            'field'    => 'id',
            'terms'    => array( $get_insu_id ),
        ),  
    ),
);
$the_query = new WP_Query( $args );

这是我尝试过的。 meta_querytax_query如果我评论其中的每一个都会做得很好。例如,我评论meta_querytax_query将正常工作。但如果我用它们两个都不行。如果您对如何操作有所了解,请帮助我。

1 个答案:

答案 0 :(得分:0)

试试这个:对你有帮助......

    $result_specialties = $_REQUEST['arr_spec'];
    $result_insurance = $_REQUEST['arr_insur']; 
    $zip_code = $_REQUEST['zip_code'];

    //get term_id
    $cat_spec = get_term_by( 'name' , $result_specialties, 'specialty' );
    $get_spec_id =  $cat_spec->term_id;


    $cat_insu = get_term_by( 'name' , $result_insurance, 'insurance' );
    $get_insu_id =  $cat_insu->term_id;

    //query 2 taxonomies
    $args = array(
        'post_type' => 'sample_post', 
        'meta_key' => 'zip_code', 
        'meta_value'   => $zip_code, 
        'tax_query' => array (
        'relation' => 'OR',
            array(
                'taxonomy' => 'specialty',
                'field'    => 'id',
                'terms'    => array( $get_spec_id ),
            ),
            array(
                'taxonomy' => 'insurance',
                'field'    => 'id',
                'terms'    => array( $get_insu_id ),
            ),  
        ),
    );
    $the_query = new WP_Query( $args );