Exclude Posts from SEARCH WP REST API - wordpress

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.

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)

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.

How to sort products in WooCommerce WordPress JSON API

I am using WooCommerce JSON API on my mobile app but I am having problems sorting the the product list.
This is my url https://www.storeurl.com/wp-json/wc/v1/products but I don't know which parameters to add to the url to sort the products according to price, reviews, rating and popularity.
I have read the docs but I couldn't find it. Please do you know I could do this?
You just need to append the parameters as part of the query string like so...
https://www.storeurl.com/wc-api/v3/products?orderby=title&order=asc
To take that further, you can use the "Filter" parameter, which allows you to use any WP_Query style arguments you may want to add to your request. So for example, if you wish to sort by "Price", you would do something like...
https://www.storeurl.com/wc-api/v3/products?filter[order]=asc&filter[orderby]=meta_value_num&filter[orderby_meta_key]=_regular_price
Filter - Use WP Query arguments to modify the response; private query
vars require appropriate authorization.
Ref: https://woothemes.github.io/woocommerce-rest-api-docs/#list-all-products
After a long search I found solution.
you need to pass query Param like below
https://v1t.a8c.myftpupload.com/wp-json/wc/v3/products? consumer_key=ck_4fxx69xxx195565b4ffca55aca56d2f4d6e&consumer_secret=cs_3928b95d5xxxxxe1e82xx210748956&page=1&status=publish&category=130&per_page=1&orderby=price&order=asc.
Also you can sort by below param.
date, id, include, title, slug, modified, menu_order, price, popularity, rating
Ex: &orderby=popularity, &orderby=rating
if you still facing issue comment here i will help you.
Thanks

WordPress REST API Global Search (API V2)

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

WooCommerce Rest API get products by slug

New to WooCommerce, and using the REST API to get products which I have successfully done.
Not sure on how to get a single product using the slug as permalink gives full url, where as i only need the end slug so I can have clean urls and not use the id to find a record. I know with the Wordpress rest api plugin you can do this posts-api?filter[name]== and get the post by product slug, rather than use:
/wc-api/v3/products/id
I want to be able to do:
/wc-api/v3/products/slug
You just have to adjust the query slightly /wc-api/v3/products/slug should be /wc-api/v3/products/?slug=your-product-slug.
One important difference is that this will return the slug inside an array, so you will have to select the first object in the array once you have parsed it.

Resources