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]

  • Once you get the results from the "IN", you could just do a plain old foreach code loop to filter out the "NOT IN".
    – Demerit
    Jun 29, 2019 at 13:38
  • 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: github.com/civicrm/org.civicrm.api4/issues/128
    – Demerit
    Jul 1, 2019 at 3:32
  • 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?
    – repertor
    Jul 1, 2019 at 15:30
  • 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. github.com/civicrm/civicrm-core/blob/5.14.2/CRM/Contact/Form/… (see - only 80 lines of code!)
    – Demerit
    Jul 1, 2019 at 16:11


Your Answer

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

Browse other questions tagged or ask your own question.