Get posts for a specific category
-
Hi,
How can I get the posts for a specific category?
Was trying
/wp-json/wp/v2/posts/categories/belgiumbut does not work.Any help? Thanks!
-
Try
/wp-json/wp/v2/posts/?filter[category_name]=belgiumWorks great!
Thank! 🙂Any idea how can I print the post titles on another website, with PHP (or jquery)?
Trying this (just to show an alert window to test), but not working:
<script> (function($) { $.getJSON('http://mywebsitedomain.com/wp-json/wp/v2/posts/?filter[category_name]=belgium', {format: "json"}, function(data) { $.each(data.posts, function( key, value ) { alert(value.title); }); }); })(jQuery); </script>Thanks!
The response is an array, not an object
You can iterate
$.each(data, instead of treating it like an object.You mean like this? (not working)
$.getJSON('http://interelgroup.com/wp-json/wp/v2/posts/?filter[category_name]=belgium', {format: "json"}, function(data) { $.each(data["posts"], function( key, value ) { alert(value["title"]); }); });You mean like this?
Nope.
$.getJSON('http://interelgroup.com/wp-json/wp/v2/posts/?filter[category_name]=belgium', {format: "json"}, function(data) { $.each(data, function( key, value ) { alert(value["title"]); }); });That does not comply with the structure. See this: https://db.tt/KJLprB1a
That does not comply with the structure.
Where is that screenshot from? That’s not how the Posts response is formatted in WP-API v2.
Oh, right, my bad! That’s from other plugin. Sorry! Think I got it. Will get back to you if not!
Now I am trying to create a div with each title, but no luck. :S
<script> (function($) { $.getJSON('http://interelgroup.com/wp-json/wp/v2/posts/?filter[category_name]=belgium', {format: "json"}, function(data) { $.each(data, function( key, value ) { $('<div data-index="'+key+'></div>').html(value["title"]["rendered"]).show(); }); }); })(jQuery); </script>OK, found it! Thanks for your precious help! 🙂
hello, I installed Wp Rest API, when using this method to pull post form a given category (/wp-json/wp/v2/posts/categories/=movies)
It doesn’t pull all the post from the category, can anyone help me on how to get all the post from that category ?It should be
/wp-json/wp/v2/posts/categories/movies(without the = )And have you tried
/wp-json/wp/v2/posts/?filter[category_name]=movies?
Also please double check the category slug ismoviesand notmovie-
This reply was modified 9 years, 6 months ago by
rjpedrosa.
Has the API been changed since?
I can query using the category ID but not the slug:
Works:
http://atalmataltootooleh.com/wp-json/wp/v2/posts/?categories=5
Won’t work:
http://atalmataltootooleh.com/wp-json/wp/v2/posts/?filter%5Bcategory_name%5D=story
http://atalmataltootooleh.com/wp-json/wp/v2/posts/categories/story
(Note the slug IS correct: http://atalmataltootooleh.com/category/story/) -
This reply was modified 9 years, 6 months ago by
The topic ‘Get posts for a specific category’ is closed to new replies.