0

I have a custom post type "book", and a taxonomy "language" with terms "php" , "java" ,"c", "python". I want to count "book" posts not having terms "java" & "c".. such that posts with only "java" or "c" should be counted but posts with both "java" and "c" should not be counted. I tried tax_query of WP_QUERY using operator "NOT IN" and "=" but haven't got correct answer. There should be "NAND" operator for tax_query.

P.S : I have approx 20 terms and 200 posts for each. With this tax_query I have added 1 AND tax_query also and that is for other taxonomy term.

$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');

A book may have one or more language terms.

1

There is not a NAND operator for the tax_query of WP_Query. If I understood correctly, what you intend can be rewritten this way:

  1. Get all the posts without the term c.
  2. Get all the posts without the term java.
  3. Exclude the posts having both the terms c and java.

To achieve that, you can combine two NOT IN queries and match them with an 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 );
  • Thanx for this but, I have approx 20 terms and 200 posts for each so this should not be a good way to get correct answer. – Mihir Jul 31 '16 at 15:47
  • May I ask why not? It runs a single and fairly simple SQL query achieving the desired result. If you intend to do some other kind of queries, please, update the question with as much details as possible. Otherwise it's impossible for us to know beforehand. – Luis Sanz Jul 31 '16 at 16:13
  • If I want 17 different combinations, I need to write 17 NOT IN arrays for each i.e total 289 NOT IN arrays in tax_query. Can you please provide a better way for this? Sometimes the question requires editing even after it gets an answer. It happens. – Mihir Jul 31 '16 at 16:25
  • With those requirements, I won't expect a simple solution. But let's wait then for other users to come with different approachs. Tomorrow you will probably get more answers. – Luis Sanz Jul 31 '16 at 16:40
  • I think you will do better producing a single $posts array and then using a function to iterate over its contents to produce the count contextually, rather than (apparently) through a relatively large number of separate db queries. However, it's not clear what you want on the output side. At one point it appears that you have particular exclusions in mind, but at another point it seems that you're interested in books that treat one language only, and have no use for ones that have two or more. Is that really what you mean? – CK MacLeod Jul 31 '16 at 16:54
0

Hey guys somehow this helped me to get count of NAND operation. Because I dont need ti iterate through those posts, I used found_posts of WP_Query . Subtracting my posts count from total post count.

I'm still waiting for a better way if any. 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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.