如何统计其他没有具体分类术语的帖子?

时间:2016-07-31 作者:Mihir

我有一个自定义的post类型“book”,还有一个分类“language”,其中包含“php”、“java”、“c”、“python”等术语。我想统计没有“java”术语的“book”帖子(&;“c”。。这样,只有“java”或“c”的帖子应该被统计,但同时有“java”和“c”的帖子不应该被统计。我使用运算符“NOT IN”和“=”尝试了WP\\U查询的tax\\u查询,但没有得到正确的答案。tax\\u查询应该有“NAND”操作符。

P、 我大约有20个学期,每个学期200个职位。通过这个tax\\u查询,我还添加了1和tax\\u查询,这是用于其他分类术语的

$myargs=array( \'posts_per_page\' => -1, \'tax_query\' => array( \'relation\' => \'AND\', array( \'taxonomy\' => \'author\', \'field\' => \'slug\', \'terms\' => array( \'ABC\', \'XYZ\' ), ), array( \'taxonomy\' => \'language\', \'field\' => \'slug\', \'terms\' => array( "c", "java"), \'operator\' => \'NOT IN\', ), ), \'post_type\' => \'book\');

一本书可能有一个或多个语言术语。

2 个回复
SO网友:Luis Sanz

没有NAND 操作员tax_query of WP_Query. 如果我理解正确,可以这样改写您的意图:

获取所有没有期限的帖子c.java.c 和java.NOT IN 查询并将其与OR:

$args = array(
    \'post_type\' => \'book\',
    \'tax_query\' => array(
        \'relation\' => \'OR\',
        array(
            \'taxonomy\' => \'language\',
            \'field\'    => \'slug\',
            \'terms\'    => array( \'c\' ),
            \'operator\' => \'NOT IN\',
        ),
        array(
            \'taxonomy\' => \'language\',
            \'field\'    => \'slug\',
            \'terms\'    => array( \'java\' ),
            \'operator\' => \'NOT IN\',
        ),
    )            
);

$posts = get_posts( $args );

echo count( $posts );

SO网友:Mihir

嘿,伙计们,不知怎么的,这帮我统计了NAND操作的数量。因为我不需要迭代这些帖子,我使用found_posts 属于WP_Query . 从总帖子数中减去我的帖子数。

我仍在等待更好的方法。Thanx公司Luis.

$myargs=array(
\'posts_per_page\' => -1,
\'tax_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'taxonomy\' => \'author\',
            \'field\'    => \'slug\',
            \'terms\'    =>  $author_terms //array of author term slug(s),
            \'operator\' => \'IN\'  //default operator ?

        ),
        array(
            \'taxonomy\' => \'language\',
            \'field\'    => \'slug\',
            \'terms\'    => $language_terms //array of language term slug(s)
            \'operator\' => \'AND\' 
        )
    ),
\'post_type\' => \'apk\');

$allargs=array(
\'posts_per_page\' => -1,
\'tax_query\' => array(
        array(
            \'taxonomy\' => \'author\',
            \'field\'    => \'slug\',
            \'terms\'    =>  $author_terms //array of author term slug(s),
            \'operator\' => \'IN\'  //default operator ?

        )
    ),
\'post_type\' => \'apk\');

$myquery=new WP_Query($myargs);
$allquery=new WP_Query($allargs);

echo allquery->found_posts - $myquery->found_posts;