Skip to content

Support tax_query filter (added in WP-API/WP-API#2748) - #243

Closed
kadamwhite wants to merge 1 commit into
masterfrom
tax-query
Closed

kadamwhite wants to merge 1 commit into
masterfrom
tax-query

Conversation

@kadamwhite

Copy link
Copy Markdown
Collaborator

This PR provides a convenience method for setting the tax_query filter:

// Return only posts in category 29
site.posts().taxQuery({
  taxonomy: 'category',
  terms: [ 29 ]
})...

Note that if a relation is specified, numeric indices must be provided:

// Return posts in (cats 49 or 29) && in tag 79 && NOT in tag 106
const request = site.posts().taxQuery({
  relation: 'AND',
  '0': {
    taxonomy: 'category',
    terms: [ 49, 29 ]
  },
  '1': {
    taxonomy: 'post_tag',
    // field: 'slug',
    terms: [ 79 ]
  },
  '2': {
    taxonomy: 'post_tag',
    terms: [ 106 ],
    operator: 'NOT IN'
  }
});

If "OR" can be used as the topmost relation, an array of tax_query objects can be provided to the filter method instead; however, any nested queries must use array indices because the array -> object parsing is not recursive.

// Implicit OR: this query is, all posts with
// (tag #93 && category #49) || (tag #81 && not tag #116)
site.posts().taxQuery([{
  relation: 'AND',
  '0': {
    taxonomy: 'category',
    terms: [49]
  },
  '1': {
    taxonomy: 'post_tag',
    terms: [93]
  }
}, {
  relation: 'AND',
  '0': {
    taxonomy: 'post_tag',
    terms: [81]
  },
  '1': {
    taxonomy: 'post_tag',
    terms: 116,
    operator: 'NOT IN'
  }
}]);

This PR provides a convenience method for setting the `tax_query` filter:

```js
// Return only posts in category 29
site.posts().taxQuery({
  taxonomy: 'category',
  terms: [ 29 ]
})...
```

Note that if a relation is specified, numeric indices must be provided:

```js
// Return posts in (cats 49 or 29) && in tag 79 && NOT in tag 106
const request = site.posts().taxQuery({
  relation: 'AND',
  '0': {
    taxonomy: 'category',
    terms: [ 49, 29 ]
  },
  '1': {
    taxonomy: 'post_tag',
    // field: 'slug',
    terms: [ 79 ]
  },
  '2': {
    taxonomy: 'post_tag',
    terms: [ 106 ],
    operator: 'NOT IN'
  }
});
```

_If_ "OR" can be used as the topmost relation, an array of tax_query
objects can be provided to the filter method instead; however, any
nested queries must use array indices because the array -> object
parsing is not recursive.

```js
// Implicit OR: this query is, all posts with
// (tag #93 && category #49) || (tag #81 && not tag #116)
site.posts().taxQuery([{
  relation: 'AND',
  '0': {
    taxonomy: 'category',
    terms: [49]
  },
  '1': {
    taxonomy: 'post_tag',
    terms: [93]
  }
}, {
  relation: 'AND',
  '0': {
    taxonomy: 'post_tag',
    terms: [81]
  },
  '1': {
    taxonomy: 'post_tag',
    terms: 116,
    operator: 'NOT IN'
  }
}]);
```
@kadamwhite

Copy link
Copy Markdown
Collaborator Author

Closing because WP-API/WP-API#2748 was closed as well

@kadamwhite kadamwhite closed this Oct 6, 2016
@kadamwhite
kadamwhite deleted the tax-query branch October 6, 2016 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant