WordPress REST API Global Search (API V2) - wordpress

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?

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.

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

Related

Is there any way to search products by name in WooCommerce API

I am trying to retrieve a product by name using WooCommerce API but I failed to do so.
Is there a way to find a product by its name?
Thank you.
You can retrieve a product by name using the WooCommerce REST API by using the list all products endpoint with the parameter ?search. List of the parameters here. With the list of parameters, there's quite a bit you can do with the list all products endpoint.
Your whole request url might look something like this:
.../wp-json/wc/v3/products/?search="Product Name"
The search parameter will look for a matching string in the name of the product. So if you have multiple products with the same words in them, they will also be returned.
I retrieve a product by name using the WooCommerce REST API with parameters.
I used python for it. You can use a similar approach:
def get_product_by_name():
product_list = wcapi.get("products", params={'search': {searchQuery}}).json()
count_of_items = len(product_list)
... do something else
{searchQuery} - product name(or part of name)

WPGraphQL: Custom Post Types query using SLUG as unique identifier returns no data

I've created a Custom Post Type in WordPress named "projects" and now I'm trying to query a single project using the slug as an identifier. In other words, I want the idType to be the SLUG, not ID. I tried the following query but it does not work.
However, this query works (it's a common query that is using the ID) :
The query above is not what i'm looking for, but at least i know that my CPT is registered correctly and that my data can be fetched somehow.
Why I think this is a problem?
Because I used the same type of query for querying WordPress posts (not custom posts types, just posts) and it works fine. I'm being returned the correct data.
Any help is appreciated ;) I don't know if i'm missing something with my query, or if custom post types don't work the same way that posts do.
WPGraphQL version: 1.3.10 Other WordPress plugins installed: Advanced
Custom Fields, Custom Post Type UI
Set the idType to URI. Here's an example, querying a page (it's the same for other post types as well):
NOTE: the GraphiQL IDE has some nice auto-completions and drop-downs to show the available options:
My understanding is the idType:SLUG option is not available when your custom post type is hierarchical since the slug would not be a unique identifier at that point.

Exclude Posts from SEARCH WP REST API

How can I exclude posts from the search endpoint in WP REST API?
I have:
'/wp-json/wp/v2/search?s=lorem&page=1&per_page=60&_embed&exclude=1392
where 1392 is the ID of excluding page, but it does not work...
https://developer.wordpress.org/rest-api/reference/search-results/
I can't pass exclude parameters to search results, but I have to exclude some posts from the WordPress search.
Anyone know how can I do that?
Thanks!
The exclude is an array, so to exclude the first post use exclude[0]=1392, to exclude a second post: exclude[1]=333
The test that worked for me uses this structure which is different to yours:
'/wp-json/wp/v2/posts?search=lorem&exclude[0]=1392'
Please note if you pass all the exclude ids through an array, the REST API will generate the exclude parameters positions for you.

Get all categories, custom field values in Wordpress query?

I've setup an API endpoint on a Wordpress site I'm working on, but the posts I'm retrieving have quite a bit of associated data, I think doing a query to get the posts then running the posts through a loop and getting this data will be too intensive, is there a catch all that can get all meta data at the same time as getting the posts?
If you're using REST API, you can use the function register_rest_field to add fields to responses. And then you can query all custom fields to put them in the responses.
If you're using a plugin like Meta Box to create custom fields, then you can use its free extension MB REST API to pull all the custom fields into REST API responses for you.

Algolia Wordpress plugin without Instantsearch.js - filtering results by taxonomy

The Algolia Wordpress plugin (https://community.algolia.com/wordpress/) replaces the standard Wordpress search.
I've selected the Use Algolia in the backend setting instead of Use Algolia with Instantsearch.js, because I need complete control over the UI.
This option states that it does not support filtering and displaying instant search results. However, I'm assuming this is referring to Instantsearch.js and there is still a way to filter the API query manually.
I need to either:
Filter the Algolia API search results by taxonomy, likely with facets. Can I customise the query
that is sent to the Algolia API to include facet filtering?
If that's not possible, is there a plugin filter that would allow me to filter the
results (and adjust the global WP_Query) after the API query has
completed?
Thanks in advance.
There is a filter hook that can be used to adjust the parameters used for the call to Algolia: algolia_search_params.
This filter is called right before doing the search operation: https://github.com/algolia/algoliasearch-wordpress/blob/master/includes/class-algolia-search.php#L59
By using that filter you can provide any valid Algolia search parameter.

Resources