Wordpress 查询帖子分类法 [英] Wordpress Query Posts Taxonomy

查看:34
本文介绍了Wordpress 查询帖子分类法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在我的 wordpress 网站上有一个基于复选框的搜索和过滤器.

基本上它使用这个 wp_query

<前>$queryObject = new WP_Query(array("post_type"=>'toy','posts_per_page'=>999999,'category__and' => $_POST['myListOfCategories']));

我想放弃使用类别(因为它使使用博客变得痛苦)

所以我现在使用自定义分类法设置了所有内容,并注意到没有taxonomy__and".

有谁知道使用 WP_Query 以与 category__and 和工作方式相同的方式使用分类法搜索帖子的方法?

例如我传递了多个分类 ID,它只返回所有这些都链接的帖子.

解决方案

在其他海报的帮助下,这里是我问题的最终答案:

<前>$queryObject = new WP_Query(array("post_type"=>'toy','posts_per_page'=>10,'tax_query' => array(大批('分类' => 'toy_cats','字段' => 'id','条款' => 数组(14,20,39,42),'运算符' => 'AND' ))));

上面的代码将只显示具有玩具"帖子类型的帖子,在分类toy_cats"中,并被分配给所有以下术语 id 的 14 AND 20 AND 39 AND 42

希望这对其他人有帮助.

I currently have a checkbox based search and filter on my wordpress site.

Basically it works using this wp_query

$queryObject = new WP_Query(array("post_type"=>'toy','posts_per_page'=>999999, 
    'category__and' => $_POST['myListOfCategories']));

I want to move away from using categories (as it's making using the blog a pain)

So I've set everything up using custom taxonomies now and notice there is no "taxonomy__and".

Does anyone know of a way to use WP_Query to search for posts using taxonomies in the same way category__and and works?

e.g. I pass multiple taxonomy id's and it only returns posts which have all of them linked.

解决方案

With the help of the other posters here is the final answer to my question:

$queryObject = new WP_Query(array("post_type"=>'toy','posts_per_page'=>10,'tax_query' => array(
        array(
            'taxonomy' => 'toy_cats',
            'field' => 'id',
            'terms' =>  array(14,20,39,42),
            'operator' => 'AND' )
    )));

The above code will only show posts that have the "toy" post type, is in the taxonomy "toy_cats" and is is assigned to ALL the following term id's 14 AND 20 AND 39 AND 42

Hope this helps someone else.

这篇关于Wordpress 查询帖子分类法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆