Include custom fields in WordPress REST API search results - php

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/.

Related

WP plugin External API, Custom Post Type

I have set up my WordPress plugin using a boilerplate. I have created a custom post type for my plugin. This is where I am at.
Where I am trying to go..
I would like to be able to create post that I have made. It is a reports Post. So in the create, I would like to customize the user interface. I want to be able to populate the options based on an external api call. One selection will populate the next selection using another external api call based on what it returned.
Then create the post.
When clicking on the post I would like to be able to use the metadata/data that came from the creation of the post type to make several api calls and populate a spreadsheet that will be displayed on the page.
I have been looking for tutorials for this and have not been able to find any. I have looked over the wp plugin development handbook and do not see a clear picture on how I can do this.
Is it possible to implement this way or should I try to make everything custom (which will be a headache especially when trying to comply to the rules, this is something I want to publish to the wordpress plugin repository eventually).
I would also like this to have a similar functionality on the front end.
Here is the process I would like the user to have.
Custom Reports page loads
-> lists all of the reports created and has a button to create a new report
-> create new report page has drop down lists. The first drop down list has x amount of options that are populated using an external api with a header that has the users credentials within it, the user selects one option. The next dropdown list is populated by an api get request using the selection from the first dropdown list. The next dropdown list is populated using an external api get request's response from the selection from the second dropdown list and so on..
-> would like to apply filters but that piece can come later
-> The user generates the report
->Then the user saves the report
When clicking on a report within the reports page, a report will be generated using the saved selections from the create report. This generation will be handled by api calls using the selections as headers. The response will then be parsed through and filled into a spreadsheet that will be displayed for the user.
The user can then export the report into a pdf or csv file.
My main question right now is there a way to implement this type of logic using custom post types within wordpress?

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?
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.

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.

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.

Performing wordpress search from a plugin

What's the correct way of performing an article search from within a Wordpress plugin? I want to perform the search and get a list of matching posts and pages back. That is I want the list back in the plugin code for further processing. I keep thinking there should be a function I can call, but I can't find it.
I have not worked with this plugin and I'm not entirely sure whether it serves search results in the way you need, but I think it does. Take a look at Wordpress Search API plugin.

Categories