【问题标题】:WordPress: get all posts included in two taxonomies (WP_Query tax_query relation AND)WordPress:获取包含在两个分类法中的所有帖子(WP_Query tax_query 关系 AND)
【发布时间】:2022-01-17 21:18:44
【问题描述】:

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

有多种分类法,例如locationset。我需要输出两个分类中包含的帖子数量。我的函数现在看起来像这样:

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-&gt;term_id,类别ID 来自location 分类。 3031 销售和租赁类别来自 set 分类。

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

【问题讨论】:

  • 你是如何得到 $id, $setid 的? $setid 应该始终保持 30 和 31 对吗?因此,如果您目前在位置 A,您想查看该位置有多少房产可供出租和出售?
  • 对。 $id 我们从当前位置获取 - 给出正确的值。 $setid = 30 或 31。会有其他的 $setids,但我检查了它们,出了点问题)

标签: php wordpress function taxonomy


【解决方案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>

【讨论】:

    猜你喜欢
    • 2014-12-05
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多