Body: <p>I can do a custom search on Contacts to include those with a list of tags, and exclude those that also have an additional tag. And I get the results I expect.</p>
  
  <p>I can't for the life of me figure out how to do this with the API, though. The API Explorer won't allow me to add 'tags' as a parameter twice, so I can't figure out how to do the construction.</p>
  
  <p>For example, I want to match all contacts that have tag ID 12, but exclude those that also have tag ID 7.</p>
  
  <p>Something like
  <code>'tag' =&gt; ['IN' =&gt; $tags_included, 'NOT IN' =&gt; $tags_excluded]</code></p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-06-29T13:38:11.537Z
Comments.Text: Once you get the results from the "IN", you could just do a plain old `foreach` code loop to filter out the "NOT IN".
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-07-01T03:32:12.700Z
Comments.Text: Interestingly the api4 explorer (/civicrm/api4) appears to make this very easy, except I can't get it to work and I always get 0 results. Possibly related to this issue, which is different but might be a similar cause: https://github.com/civicrm/org.civicrm.api4/issues/128
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-07-01T15:30:23.273Z
Comments.Text: And I can do this through a custom search easily, as well. How does the custom search function? Does it not use the API internally?
Comments.UserDisplayName: repertor
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-07-01T16:11:56.727Z
Comments.Text: The custom search does some procedural steps creating a temporary table and raw SQL. It does not use api3. In fact I'm sure it was written before api3, and possibly even api2. https://github.com/civicrm/civicrm-core/blob/5.14.2/CRM/Contact/Form/Search/Custom/Group.php#L399-L478 (see - only 80 lines of code!)
Comments.UserDisplayName: Demerit
Tags: <api><tags>
Title: API v3 Include Some Tags and Exclude Others
ThreadText: # API v3 Include Some Tags and Exclude Others
  
  (Asked by: username_0 on 2019-06-29)
  
  I can do a custom search on Contacts to include those with a list of tags, and exclude those that also have an additional tag. And I get the results I expect.
  
  I can't for the life of me figure out how to do this with the API, though. The API Explorer won't allow me to add 'tags' as a parameter twice, so I can't figure out how to do the construction.
  
  For example, I want to match all contacts that have tag ID 12, but exclude those that also have tag ID 7.
  
  Something like
  `'tag' => ['IN' => $tags_included, 'NOT IN' => $tags_excluded]`
  
  **Tags:** api, tags
  
  > **Comments:**
  > **username_1**: Once you get the results from the "IN", you could just do a plain old `foreach` code loop to filter out the "NOT IN".  
  > **username_1**: Interestingly the api4 explorer (/civicrm/api4) appears to make this very easy, except I can't get it to work and I always get 0 results. Possibly related to this issue, which is different but might be a similar cause: https://github.com/civicrm/org.civicrm.api4/issues/128  
  > **username_0**: And I can do this through a custom search easily, as well. How does the custom search function? Does it not use the API internally?  
  > **username_1**: The custom search does some procedural steps creating a temporary table and raw SQL. It does not use api3. In fact I'm sure it was written before api3, and possibly even api2. https://github.com/civicrm/civicrm-core/blob/5.14.2/CRM/Contact/Form/Search/Custom/Group.php#L399-L478 (see - only 80 lines of code!)  
  
  ---
  
  ## 0 answers
  
  