Lesson 1: Searching WordPress data
Searching for data within WordPress is limited in several cases, and Gato GraphQL can help augment these capabilities.
Such an example involves custom fields (i.e. meta values): We may use custom fields to add extra information to posts (and also to users, comments, and taxonomies), however when searching for posts with some keyword, WordPress does not search within meta values.
We can then use Gato GraphQL to search for posts (and also users, comments, and taxonomies) by meta key and value.
Examples
- Create the queries below as Persisted Queries, as to keep them stored in the website, and execute them time and again
- Publish them as
private
, so that they are available only within the wp-admin, and only to the admin - Use an API hierarchy to manage them (eg: have a Persisted Query
internal
be the ancestor to all the internal queries:internal/search-posts-without-thumbnail
,internal/search-users-by-locale
, etc)
This query retrieves all posts that have a thumbnail, and those that do not:
This query retrieves all users who use the locale "Spanish from Argentina":
We can use the AND
and OR
relations to filter data more precisely. This query retrieves posts that both have a thumbnail, and also have a custom meta todo_action
with value "replace"
(meaning that it needs to have the thumbnail replaced):
Filtering by meta can also be combined with any of the standard data items. This query retrieves all posts without thumbnail that were created after a certain date and have been tagged "wordpress"
:
We can also search meta using regex expressions. This query searches for all users with a Spanish locale (for instance, es_AR
for Argentina, es_ES
for Spain, and so on):