Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use WordPress REST API v2 to search posts?

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?

like image 796
clzola Avatar asked Sep 08 '17 09:09

clzola


1 Answers

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

like image 58
daniel rubambura Avatar answered Sep 23 '22 18:09

daniel rubambura