How to use WordPress REST API v2 to search posts? - php

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?

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

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.

Related

How to sort post randomly with WP REST API?

I am trying to display posts using the WP REST API and I want to display them randomly.
I know querying the posts the normal way with wordpress there is a orderby: rand option, which would do what I am looking for, but I can't figure it out with the REST API.
My REST API looks like this:
https://example.com/wp-json/wp/v2/fatos_random?filter[orderby]=rand&per_page=1
thanks for help

How to filter post types with a select field?

I'm working on a Wordpress project and have a question.
I have a layout like shown below in the link to screenshot with a select field and boxes with news (two or more different post types). I want to filter with the select the post type so there should show up only the selected. How can i do that?
Thanks in advance.
If you're fine dropping the pagination and having all of your posts on a single page, one option is to use Isotope.js. In addition to being a very useful plugin, they have thorough documentation to figure out how to structure your code to get filtering working.
If the pagination is absolutely necessary, this becomes more complex. Here's the short hand-wavey version that needs testing: You can use JS to reload the page with an extra parameter added to the URL (ex: /your-page?filter=post_type_1). You'll need to register your custom URL parameter with WordPress. Then, in your PHP, you'll use that parameter data in your WP_Query (or whatever you're using to query posts) to grab only posts of that type. It would be smart to have a default, where if the filter parameter from the URL isn't present or doesn't match a post type you have in your site, you default to showing all posts.
This blog post walks through how to do this method of filtering more thoroughly: https://codepen.io/the_ruther4d/post/custom-query-string-vars-in-wordpress.

Include custom fields in WordPress REST API search results

I am working on an application using the WP REST API to display data. I need to be able to search through data entered as a custom field. (I am using Advanced Custom Fields). I have expanded the REST API so that I can grab custom fields using the API. Now I need to include those fields within the searchable data for the post.
I want to search through an HTTP request:
http://example.com/wp-json/posts?type[]=post&filter[posts_per_page]=5&filter[s]=searchterm
In the above example, searchterm is the term that is being searched for. The above request works, but it will only search within title and content. So if searchterm is within either of those, then the search works but if searchterm is within a custom field then the search does not work.
Is there a way that I can expand the search function of the REST API so that I can search through custom fields?
Thanks!
I was able to fix this for the REST API V1 by using the Search Everything plugin - https://wordpress.org/plugins/search-everything/.

How can I search the post title and tag using WP_query in wordpress?

I am currently implementing a search function to search the post from wordpress
I would like to search the post by Tag , by title and return it as JSON
I found a plugin that generates the result as JSON (JSON API)
http://wordpress.org/plugins/json-api/other_notes/#2.1.-Core-controller-methods
In that plugin , there is a get_posts function that can return the query that supports WP_query parameter
I tried a query like that
http://MyWordpressSite.com/?json=get_posts&status=published&orderby=date&order=DESC&lang=en&s=eng&tag=eng&cat=9,10,14,15
where the &s= is the keyword to search while the &tag= is the tag to search
However, there are two problems
How can I limit the search to title only?
http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
From the official website, it state search keyword ,but not limit to title only
It seems the condition is tag "AND" keywords are match, but what I would like to achieve is tag "OR" keywords are match
http://codex.wordpress.org/Function_Reference/get_posts
So, to sum up , the goal is to search the post by Tag , by title and return it as JSON (actually is call the php through JSON), it doesnt matter using the plugin or not, any way achieve the goal is welcomed. Thanks for helping
I looked into plugin functions, This plugin does not support to get posts from multiple category. It just get one category by ID and return the posts and this category info. You need to make a custom plugin or try another to do this task.

Count of result sites in google search and rivalry level of query

I'm working with PHP web site and I need to get count of sites for custom expression in Google search. Also I need to get rivality level [0..1] of this custom expression (query).
I've searching for something like this in Google Custom Search API but didn't find.
Thanx for helping.

Categories