8

I am writing Android application and I am using WordPress REST API v2 for communication with WordPress from my Android application. I have implemented Activity that lists all posts from single category:

http://wordpress.dev/wp-json/wp/v2/posts?categories=X

But now I am adding search box in my application where user can enter some text and now I need to use this text to perform search over all categories

I can use same endpoint but different parameters:

http://wordpress.dev/wp-json/wp/v2/posts?search=<SEARCH TEXT>

In my local wordpress database I have a lot of posts that are called "POST TITLE #XY" and one that is called "Cover Photo"

So if I execute this:

http://wordpress.dev/wp-json/wp/v2/posts?search=title,post

I get only those that has post and title in the title, however if I add one more word:

http://wordpress.dev/wp-json/wp/v2/posts?search=title,post,cover

I get zero posts...

How can I perform search and get posts that includes one or more keywords that I am sending in request?

clzola
  • 1,925
  • 3
  • 30
  • 49

2 Answers2

7

You can use Search Results REST API Endpoint to seach by keywords you provide.

For example http://yourdomain.com/index.php/wp-json/wp/v2/search?search=lorem&per_page=5&page=1 will search for the keyword lorem in your wordpress posts.

Per_page : limits the number of results per page

daniel rubambura
  • 545
  • 6
  • 12
2

I did something similar to what you are describing for a WP/bbPress android/ios app. First, I made my own wordpress search api end point to hit. Then on the wordpress side I used a custom mysql query to query post titles and post content for the search string and return that to the api.

I made the query only return 25 results as I didn't need to have more results than that, with pagination and such.

mediaguru
  • 1,807
  • 18
  • 24