Specify the categories parameter once for each category ID to AND with others:
example.com/wp-json/wp/v2/posts?categories=1&categories=2
Note the parameter is plural “categories” even though you can only pass a single ID for each occurrence to get AND logic. Also note that URL parameters are separated from the URL with ?
, but subsequent parameters are separated from each other with &
. ?&
do not belong together.
Goodmorning (amsterdam timezone) @bcworkz,
Ah thanks for correcting the typos in the above! Also thanks for suggesting a solution to my problem. However I’m afraid this does not work out of the box. If you combine several identical taxonomies (say categories) with &
the API only seems to return posts containing the last parameter.
Here’s a specific reference to my test site:
http://placeholder-b.template-studio.nl/wp-json/wp/v2/posts?categories=47&categories=48
This request will return 3 posts, however it only takes into account the last parameter categories=48
and therefore you’ll also get results from other linked categories (in the first returned post you for example see the category with id 49) and not just posts containing category 47 AND
48
What’s your take on this?
Thanks,
Lasse
Ah, you’re right. My dataset wasn’t adequate for proper testing. Well, phooey, I’d thought I’d stumbled upon something. Sorry for the false hopes. I’m not seeing any way to get AND logic with a default API request. Some additional coding is required. Either parse through the OR’d results and extract the ones that meet AND criteria, or create a custom endpoint that applies proper logic to the query. Alternately, add a query filter that restructures the API request, which would then disable equivalent OR requests.
How would one add the ability to comment on posts? Not sure how to construct the argument or whatever it’s called? http://mysite.com/wp-json/wp/v2/posts (then comments somehow?) ?
@jrunfitpro – please start a new topic for your question. Tagging onto other members topics is strongly discouraged in these forums. (we realize it’s common practice in other forums, you are forgiven for not knowing local practice)
In your new topic, besides what you have already asked, please clarify what you wish to do with comments via the REST API. Are your trying to post a new comment, or list existing comments for a post, or list a single comment?
Thanks
Relax. I couldn’t find the new topic section. Is this how your support generally is?
Actually, my post is along this topic too.
-
This reply was modified 6 years, 4 months ago by jrunfitpro.
Helloo,
@bcworkz thanks a bunch for your effort! Good to have this cleared up. I’ll look into the hints you dropped in the above.
@jrunfitpro, @bcworkz is right. You should start a new topic for your question. It relates to creating comments using the REST API . You can start a new topic on this issue if you scroll all the way to the bottom of this forum: https://wordpress.org/support/forum/wp-advanced/.
A note on support: the WordPress-ecosystem is highly driven by volunteers and open source enthusiasts. When asking for support you’re relying on people to spend their own free time helping you.
All the best
Lasse
With the amount of effort you two have put into this, you could’ve answered this by now. Just saying.