Marc
Marc

Reputation: 4851

WordPress REST API Global Search (API V2)

I am looking for a way to run a global search query across all or multiple post types using WP REST API.

I am able to search posts using:

http://example.com/wp-json/wp/v2/posts?search=test

In turn I am able to search pages using:

http://example.com/wp-json/wp/v2/pages?search=test

How do I search across both posts and pages? I was able to do this in WP REST API V1 by specifying multiple type[] variables in the query?

Upvotes: 16

Views: 26711

Answers (2)

Marc
Marc

Reputation: 4851

Here are some examples using the REST API search endpoint for searching all content or specific content type(s).

Search For a Term in All Site Content

/wp-json/wp/v2/search/?search=searchterm

Search For a Term and Limit Results to a Custom Post Type

/wp-json/wp/v2/search/?subtype=book&search=searchterm

Search For a Term and Limit Results to multiple Custom Post Types

/wp-json/wp/v2/search/?subtype[]=book&subtype[]=movie&search=searchterm

REST API Search Result Documentation

Upvotes: 5

Consti P
Consti P

Reputation: 455

This might be a bit late but there is an endpoint for that in the v2-api: /wp-json/wp/v2/search.

You can search for any specific post_type by supplying it via subtype or leave it to the default (any) to search in all post_types.

 

Edit: Of course you can also specifiy multiple with an array as you did before.

Upvotes: 16

Related Questions