How to query all posts using "meta_key" and "orderby"?

Hi, I followed this tutorial to start counting post views in Frontity.
The request in every post view is working fine, however I’m kinda lost as how to return the top 4 most-read posts.
The tutorial final step says “Now it’s easy to make a query for the most popular posts by including ‘meta_key’ => ‘views’ and ‘orderby’ => ‘meta_value_num’ as query arguments.”

But I don’t know how to do this.

I need to retrieve the 4 most popular posts so I can list them in the homepage. It would be good too if I could get only recent (2 weeks ago to current day) posts, to achieve kind of a “trending new posts” effect.

I tried searching the docs but maybe I’m searching for the wrong terms.

I’m not sure if this is a complete answer, but maybe it’ll help somewhat. I tried to query posts from my testing environment, using the meta-keys as well. I ended up with a request url like this:
https://frontity.noesteprojecten.nl/wp-json/wp/v2/posts?meta_key=_portfolio_url&meta_value=field_5e35702ed4b1c&filter[orderby]=_portfolio_url&order=desc

So I guess your url could look something like this:
https://[website]/wp-json/wp/v2/[post_type]?meta_key=[meta_key_name]&meta_value=[meta_key_value]&filter[orderby]=[meta_key_value]&order=desc

But maybe you already fabricated something like this as well and are just unsure on how to use the returned posts in Frontity. That I wouldn’t know as well (yet).

2 Likes

Yes, this is really some work to do that is not directly Frontity related but more WordPress related. One of the challenges of working with Frontity is being able of getting the info we need of WordPress via REST API.

Besides the one you mention

Here you have some resources that may be helpful for this:

Hope this helps

Hey @brunovaz.dev , did you solve this?

I’m also using the exact article for getting popular post, but stuck on how to query the posts.