繁体   English   中英

WordPress:获取包含在两个分类法中的所有帖子(WP_Query tax_query 关系 AND)

[英]WordPress: get all posts included in two taxonomies (WP_Query tax_query relation AND)

当然,这可以算作重复,但我无论如何都无法完成这个任务,我只是在学习)

有几种分类法,例如locationset 我需要 output 两个分类中包含的帖子数。 我的 function 现在看起来像这样:

function get_all_property_count($id, $setid)
{

    $count = new WP_Query(array(
        'nopaging' => true,
        'post_type' => 'property',
        'post_status' => 'publish',
        'tax_query' => array(
            'relation' => 'OR',
            array(
                'taxonomy'          => 'location',
                'field'             => 'id',
                'fields'            => 'ids',
                'terms'             => $id,
            ),
            array(
                'relation' => 'AND',
                array(
                    'taxonomy'          => 'set',
                    'field'             => 'id',
                    'fields'            => 'ids',
                    'terms'             => $setid,
                    'include_children'  => true,
                )
            )
        ),
    ));
    return $count->post_count;
}

然后我用它来获取当前分类中发布的帖子的计数器,以及另一个按 id 发布的帖子(我不确定我是否解释正确()。

<div class="count_property">
    <div class="sell">
        <span class="strong">Sell:</span> <?php echo get_all_property_count($term->term_id, 30); ?>
    </div>
    <div class="rent">
        <span class="strong">Rent:</span> <?php echo get_all_property_count($term->term_id, 31); ?>
    </div>
</div>

其中$term->term_id ,来自location分类的类别 ID。 set分类法中的3031个销售和租赁类别。

在出口处,我又得到了一个sell 比如Sell: 2 (虽然一般情况下应该是0), Rent: 1 (没错)。

尝试以下解决方案

function get_post_count_by_term_slug($post_type,$taxonomy,$term_slug) {
    $current_obj = get_queried_object();
    // Check if we are on specific taxonomy page like location
    if(!is_tax('location')) return;    

    $args = array(
        'post_type' => $post_type,
        'posts_per_page' => -1,
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => $current_obj->taxonomy, // Here we get current location
                'field' => 'id',
                'terms' => $current_obj->term_id
            ),
            array(
            'taxonomy' => $taxonomy,
            'field' => 'slug',
            'terms' => $term_slug
            )
        ),
    );
    $count = new WP_Query($args);
    if($count->have_posts()):
        echo $count->post_count;
    else: 
        echo 'Nothing found';
    endif;
}

// Use it like this get_post_count_by_term_slug('post_type','taxonomy_name','tax_term_slug');

<div class="count_property">
    <div class="sell">
        <span class="strong">Sell:</span> <?php get_post_count_by_term_slug('property','set','sell'); ?>
    </div>
    <div class="rent">
        <span class="strong">Rent:</span> <?php get_post_count_by_term_slug('property','set','rent'); ?>
    </div>
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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