【问题标题】:Query posts by category and tag_id按类别和tag_id查询帖子
【发布时间】:2026-02-08 23:25:01
【问题描述】:

我有一个如下所示的查询:

    <?php
    $args = array( 
      'post_type'=> 'user',
       'showposts'=> -1

       );

        $users = new WP_Query($args);

        echo '<pre>' .print_r($users->posts, 1). '</pre>';

        ?>

查询返回所有存在的帖子。我现在想添加一个过滤器来按类别搜索。请注意,这是自定义帖子类型,并且类别是分类法。

当我将鼠标悬停在 wordpress 中的类别上时,它会显示相关的 id。但是,当我尝试按该 ID 过滤时,它不起作用:(

有什么想法吗?

【问题讨论】:

  • 那么到目前为止,您尝试过什么。添加您的代码,即使它不能作为开始的地方。
  • 当我将鼠标悬停在 Wordpress 中的某个类别上时,它会显示 &taxonomy=user_category&tag_ID=17。所以我然后尝试按 cat = 17 过滤,我尝试了 tag_id = 17。但都没有奏效..

标签: php wordpress


【解决方案1】:

这将查询具有 ID 1、2 和 3 的 category_user 分类定义的类别的“用户”类型的帖子:

$args = array(
    'post_type'=> 'user',
    'showposts'=> -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'category_user',
            'terms' => array(1,2,3)
        )
    )
);
$users = new WP_Query($args);

阅读更多:http://codex.wordpress.org/Function_Reference/WP_Query#Taxonomy_Parameters

【讨论】: