How to sort post randomly with WP REST API? - php

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

Related

group by category with 5 post of each category in wordpress REST api single Request

I am not sure if this has been answered before, but vigorous googling has not led me anywhere so far.
I have a Wordpress site where I would like to display all posts, as usual, ordered by date. However, instead of setting a limit on the total number of posts displayed, I would like to set a limit on each category in a single API request. I am using WP version 5.2.
What is the best, most clean and maintainable way to achieve this?
I would be very grateful for any help.

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.

Fetch products by category in Woocommerce REST API

I'm using Woocomerce for my wordpress shopping cart. I'm connect my Android App to my Wordpress database through Woocommerce REST API. I need to get products by category on my App. There is no provision in the REST API to fetch product by category.
I tried by using the filter attribute:
https://example.com/wc-api/v1/products?filter[product_cat]=gedgets&consumer_key=ck_9354534x&consumer_secret=cs_dx7345345
This is return me all the products.
In this case you should use the filters...
however, It worked for me :
http://www.example.com/wp-json/wc/v1/products?category=16
the 16 is a sample of category id...
According to the WooThemes/WooCommerce documentation you need to use filters, like you say, only you appear to have the filter name wrong.
Try:
https://mysite.com/wc-api/v1/products?filter[categories]=gedgets&consumer_key=ck_9354534x&consumer_secret=cs_dx7345345
categories is the name of the field returned by the API, so that should be what you use.
You Need WP Rest API plugin for this API
Create Credentials in woocommmerce->Settings->Advance you find REST API link after tabs
add key and use Consumer Key and Consumer Secret for type OAuth 1.0 authorization header and use this API.
http://yourdomain.com/wp-json/wc/v3/products/?category={ID}
I used this API and it's perfectly working
Just in case anyone is trying to achieve this in the future with v2 of the Woo REST API, this is how it's done:
https://${baseUrl}/wp-json/wc/v2/products?category=${categoryId}
And if you only have the slug of the category, as opposed to the id, you can first:
https://${baseUrl}/wp-json/wc/v2/products/categories?slug=${categorySlug}
use filter[category] insted of filter[categories]
https://ursite.com/wc-api/v1/products?filter[category]=gedgets&consumer_key=ck_9354534x&consumer_secret=cs_dx7345345
if you are looking products filter by multiple categories id use below API endpoint:
https://example.com/wp-json/wp/v2/posts?categories=20,30
Could it be a typo in your filtering?
I noticed that it says [categories]=gedgets but should it be [categories]=gadgets instead?
I am trying to figure out Woocommerce REST API myself and I noticed that if I type something wrong in the filter, it returns all results.
According to the Woocommerce REST API, They don't have a solution(filters) for this issue. woocommerce github
Hope they will add this feture in their next release.
Actually, according to the github page, this feature is not yet available. However, if you look here: http://justgeeks.in/woocommerce-rest-api-fetch-products-category/ you will find what you need to filter by category slug...

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.

How to get list of categories of a wordpress blog using wordpress REST api

First of all i know it is a theoretical question and it contains no code but actually this time i don't have code, i need your suggestion on this issue.
Question:
Is it possible to fetch the name of all categories from a wordpress.com blog or a self-hosted blog connected through jetpack?
I tried finding it here https://developer.wordpress.com/docs/api/ but no luck.
So please suggest.
I've been looking for the same thing. I don't think this feature is available yet (As of 24th May 2013) on jetpack but available on hosted blogs (see the link below).
http://codex.wordpress.org/Function_Reference/get_categories
In case you are using JSON API plugin, you can use
http://yourdomain.com/?json=get_category_index to get the list of all categories.
PS: You can use this JSON API plugin in place of jetpack api as it is hosted on your own blog with greater control.

Categories