Custom Queries
-
Hi,
I have created several projects with different categories.Lets say I like to get all projects with the category work.
I have tried something like this:
$posts = new WP_Query(array( 'category_name' => 'work' ));or:
$args = array( 'category_name' => 'work', ); $posts = get_posts($args) ;However in the result i get anytime all posts?
What am I missing, please?
~Marc
-
Hi,
I have created several projects with different categories.Lets say I like to get all projects with the category work.
I have tried something like this:
$posts = new WP_Query(array( 'category_name' => 'work' ));or:
$args = array( 'category_name' => 'work', ); $posts = get_posts($args) ;However in the result i get anytime all posts?
What am I missing, please?
~Marc
-
well lay theme is fetching content using wp api http://v2.wp-api.org/, most of the frontend is a javascript application (minified). I'm not sure if you can do much with php here.
Let's say you have a page with an id of 1, and you visit it, then this code would fetch all projects. If you want to fetch only projects of a certain category use "http://yourwebsite.com/wp-json/wp/v2/posts?filter[category_name]=home" as your url
http://laytheme.com/documentation.html#custom-javascript
<script> Frontend.GlobalEvents.on("newpageshown", function(layoutObj, type, obj){ if(type == "page" && obj.id == 1){ //fetch all projects jQuery.ajax({ url: "http://yourwebsite.com/wp-json/wp/v2/posts" }).done(function(result) { console.log(result); }); } }); </script> -
-
Is there a way to get also the author when using something like this please?
"http://yourwebsite.com/wp-json/wp/v2/posts?filter[category_name]=home"
Or do I need a second query for this ?
-
Good question, if the author is not part of the /posts response, you can add the author by modifying the response.
http://v2.wp-api.org/extending/modifying/Here is an example:
http://v2.wp-api.org/extending/modifying/#examples -
-
Hey you could extend the post response with a field like "categories" that contains the posts categories using that function for example: https://developer.wordpress.org/reference/functions/get_the_category/
here's how to extend the response
http://v2.wp-api.org/extending/modifying/#examples -
-
Ahw, yeah try:
register_api_field instead of register_rest_field, just change the name.
i have to update the wpapi that comes bundled with lay theme, then register_rest_field will work. until then use register_api_field, its the same function
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login