简体   繁体   中英

Combination of tax_query and meta_query not get posts

I am trying to get posts using combination of meta_query and tax_query but i could not get posts using combined both.

Following is my code.

<?php 
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array(
                        'post_type' => 'tm-property',
                        'posts_per_page'=>-1,
                        'meta_or_tax' => TRUE,                           
                        'paged' => $paged,
                        'order'             => 'DESC',

                        'tax_query' => array(
                                            array(
                                            'taxonomy' => 'tm-property_type',
                                            'field' => 'term_id',
                                            'terms' => $id
                                             )
                                    ),


                        'meta_query'       => array(
                        'relation'    => 'AND',                      
                                array(
                                    'key'          => '_tm_property_bedrooms',
                                    'value'        => $min_bed,
                                    'compare'      => '=',
                                ),

                                array(
                                    'key'          => '_tm_property_bathrooms',
                                    'value'        => $min_bath,
                                    'compare'      => '=',
                                ),                                                  
                    ),                                
            );

            $custom_posts = new WP_Query( $args );

            ?>

I am getting blank array.

Also i want list of that post that does not have any taxonomy.

For eg i have 5 posts, 2 post have taxonomy and other 3 does not have any taxonomy.

so when select taxonomy name then it'll display selected taxonomy posts and when i not select taxonomy only select meta values then it will display posts of not have taxonomy.

Is it possible to get post using combination of meta_query and tax_query ?

please try this below argument for query

<?php
$category_object        = get_queried_object();
$category_taxonomy      = $category_object->taxonomy;
$category_term_id       = $category_object->term_id;

$min_bed = array( '1000', '4000' );
$min_bath = array( '2000', '3200' );

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'type'              => 'tm-property',
    'post_status'       => 'publish',
    'posts_per_page'    => 10,
    'paged'              => $paged, 
    'caller_get_posts'  => -1,
    'meta_query'        => array(
                                'relation' => 'AND',                      
                                array(
                                    'key'          => '_tm_property_bedrooms',
                                    'value'        => $min_bed,
                                    'type'    => 'numeric',
                                    'compare' => 'BETWEEN',
                                ),
                                array(
                                    'key'     => '_tm_property_bathrooms',
                                    'value'   => $min_bath,
                                    'type'    => 'numeric',
                                    'compare' => 'BETWEEN',
                                ),                                                  
                            ),      
    'tax_query'         => array(
                            array(
                            'taxonomy' => 'tm-property_type',
                            'field' => 'id',
                            'terms' => $category_term_id
                             )
                        ),   
    'orderby'           => 'name',               
    'order'             => 'DESC',                      
);
$custom_posts = null;
$custom_posts = new WP_Query($args);

if( $custom_posts->have_posts() ) :
    while ($custom_posts->have_posts()) : $custom_posts->the_post(); 
        echo get_the_title( $post->ID );
    endwhile;
endif;
wp_reset_query($custom_posts);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM