Meta Values

📣 Note: To fetch and filter by meta values, their meta keys must be added to the allowlist.

We can retrieve meta values for custom posts, users, comments, and taxonomies (tags and categories), by querying fields metaValue (for a single value) and metaValues (for an array of values) from the corresponding type:

  • Post.metaValue
  • Post.metaValues
  • GenericCustomPost.metaValue
  • GenericCustomPost.metaValues
  • User.metaValue
  • User.metaValues
  • Comment.metaValue
  • Comment.metaValues
  • PostTag.metaValue
  • PostTag.metaValues
  • PostCategory.metaValue
  • PostCategory.metaValues
  • GenericTag.metaValue
  • GenericTag.metaValues
  • GenericCategory.metaValue
  • GenericCategory.metaValues

These are examples of queries to fetch meta data, and filter results by meta.

Querying meta permalink

Fetch single meta value _thumbnail_id from posts:

Fetch array meta value upvotes from comments:

Filtering by meta permalink

Custom posts, comments, users and taxonomies (tags and categories) can also be filtered by meta, using the metaQuery input.

This input offers an enhancement over how the meta_query args are provided (to functions get_posts, get_users, etc), in that type validations are strictly enforced in the GraphQL schema, and only the combinations that make sense are exposed.

This is accomplished by using the "oneof" input field compareBy, which offers 4 possibilities. Depending on the chosen option, different operators can be used for the comparison:

compareBy inputPossible operators
1. keyEXISTS
2. numericValue=
3. stringValue=
4. arrayValueIN

In addition, when comparing by key, there's no need to provide input value.

We can pass several items under metaQuery, and decide if to do an AND or OR of their conditions by passing input relation on the first item in the list.

This query filters posts where meta key _thumbnail_id exists:

This query filters users where meta nickname has a certain value:

This query filters comments where meta upvotes (which is an array of integers) has either values 4 or 5: