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
-
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
Before you post:
- When using a WordPress Cache plugin, disable it or clear your cache.
- Update Lay Theme and all Lay Theme Addons
- Disable all Plugins
- Go to Lay Options → Custom CSS & HTML, click "Turn Off All Custom Code", click "Save Changes"
This often solves issues you might run into
When you post:
- Post a link to where the problem is
- Does the problem happen on Chrome, Firefox, Safari or iPhone or Android?
- If the problem is difficult to explain, post screenshots / link to a video to explain it