Answers.Body: <p>I don't think you can do this with one api call.</p>
  
  <p>Not optimal but you could do a postprocess in place of the <code>// do something</code> when the result comes.</p>
  
  <p>Better, you could create your own api call in a custom extension - see <a href="https://wiki.civicrm.org/confluence/display/CRMDOC/Create+a+Module+Extension#CreateaModuleExtension-AddanAPIfunction" rel="nofollow">https://wiki.civicrm.org/confluence/display/CRMDOC/Create+a+Module+Extension#CreateaModuleExtension-AddanAPIfunction</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-30T16:40:55.597Z
Answers.LastActivityDate: 2016-05-30T16:40:55.597Z
Answers.UserDisplayName: samuelsov
Body: <p>I would like to search the Civi API for Contacts that have both of two tags. I'm at a loss as to how to do this.  Right now if you provide multiple tags, the default is to use an <code>OR</code> operation.  For example:</p>
  
  <pre><code>CRM.api3('Contact', 'get', {
    "sequential": 1,
    "tag": [5,7]
    "return": ["tag","first_name"]
  }).done(function(result) {
    // do something
  });
  </code></pre>
  
  <p>This currently returns all contacts that have either tag 5 OR tag 7.  I want to return all contacts that have both tag 5 AND tag 7.  Is there any way to achieve this functionality?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-03-04T11:38:48.073Z
Comments.Text: Sorry, too lazy to open api explorer; just off the top of my head, shouldn't it be something like **"tag": {"IN":5,7}** ?
Comments.UserDisplayName: yurg
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-03-04T14:35:29.720Z
Comments.Text: I tried `"tag":{"IN":[5,7]}`, but that just did the same thing; it's still an `OR` of the two tags
Comments.UserDisplayName: Brad Xanti
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-03-04T18:03:59.503Z
Comments.Text: Than, may be, try Search Builder (someone has given me same advice somewhere in the past)? Mean find your contacts like this http://imgur.com/d9IPf3f (untested! but seems to be using **AND**,  according to what it says)  , add your findings in a "smart group" and than use API for querying that group? Quite complex path, but at least something..
Comments.UserDisplayName: yurg
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-03-04T18:51:24.230Z
Comments.Text: Thanks for helping me out.  That doesn't seem like a feasible solution, because it's supposed to be dynamic, i.e. you're supposed to be able to select from several tags, not just two, and find the AND between all of them.  I did look at the search builder; there's no way to use that in the AJAX API, correct?
Comments.UserDisplayName: Brad Xanti
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-03-04T19:09:33.943Z
Comments.Text: Right, it doesn't seems like a solution at all, just a workaround for a very specific issue you've described initially. Just came to mind: you may want to try so called civicrm "chaining" requests, mean, request tag 1 and request tag 2 from tag 1 results.
Comments.UserDisplayName: yurg
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-03-04T20:43:26.803Z
Comments.Text: Could you tell me how to (or link to a guide on how to) do that?  It's not very well-documented, and I can't figure out how to go about doing that.
  Thanks!
Comments.UserDisplayName: Brad Xanti
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-03-04T21:31:46.007Z
Comments.Text: I'd start from here: https://wiki.civicrm.org/confluence/display/CRMDOC/Chaining
Comments.UserDisplayName: yurg
Tags: <api><tags><javascript>
Title: Civi AJAX API Search by "AND"ing 2 Tags
ThreadText: # Civi AJAX API Search by "AND"ing 2 Tags
  
  (Asked by: username_0 on 2016-03-03)
  
  I would like to search the Civi API for Contacts that have both of two tags. I'm at a loss as to how to do this. Right now if you provide multiple tags, the default is to use an `OR` operation. For example:
  
  ```
  CRM.api3('Contact', 'get', {
    "sequential": 1,
    "tag": [5,7]
    "return": ["tag","first_name"]
  }).done(function(result) {
    // do something
  });
  
  ```
  
  This currently returns all contacts that have either tag 5 OR tag 7. I want to return all contacts that have both tag 5 AND tag 7. Is there any way to achieve this functionality?
  
  **Tags:** api, tags, javascript
  
  > **Comments:**
  > **username_1**: Sorry, too lazy to open api explorer; just off the top of my head, shouldn't it be something like **"tag": {"IN":5,7}** ?  
  > **username_0**: I tried `"tag":{"IN":[5,7]}`, but that just did the same thing; it's still an `OR` of the two tags  
  > **username_1**: Than, may be, try Search Builder (someone has given me same advice somewhere in the past)? Mean find your contacts like this http://imgur.com/d9IPf3f (untested! but seems to be using **AND**,  according to what it says)  , add your findings in a "smart group" and than use API for querying that group? Quite complex path, but at least something..  
  > **username_0**: Thanks for helping me out.  That doesn't seem like a feasible solution, because it's supposed to be dynamic, i.e. you're supposed to be able to select from several tags, not just two, and find the AND between all of them.  I did look at the search builder; there's no way to use that in the AJAX API, correct?  
  > **username_1**: Right, it doesn't seems like a solution at all, just a workaround for a very specific issue you've described initially. Just came to mind: you may want to try so called civicrm "chaining" requests, mean, request tag 1 and request tag 2 from tag 1 results.  
  > **username_0**: Could you tell me how to (or link to a guide on how to) do that?  It's not very well-documented, and I can't figure out how to go about doing that. Thanks!  
  > **username_1**: I'd start from here: https://wiki.civicrm.org/confluence/display/CRMDOC/Chaining  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2016-05-30. Score: 1
  
  I don't think you can do this with one api call.
  
  Not optimal but you could do a postprocess in place of the `// do something` when the result comes.
  
  Better, you could create your own api call in a custom extension - see <https://wiki.civicrm.org/confluence/display/CRMDOC/Create+a+Module+Extension#CreateaModuleExtension-AddanAPIfunction>
  
  
  ---
  
  