Lay Theme Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Search

    Custom Queries

    General Discussion
    2
    10
    865
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      daslicht last edited by daslicht

      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

      D 1 Reply Last reply Reply Quote 0
      • D
        daslicht @daslicht last edited by daslicht

        Selecting by id works :

        $args = array(
        	    'post_type' => 'post',
        	    'category' => '1', 
            );
        
        1 Reply Last reply Reply Quote 0
        • arminunruh
          arminunruh Global Moderator last edited by

          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>
          
          1 Reply Last reply Reply Quote 1
          • D
            daslicht last edited by

            @arminunruh said:

            http://v2.wp-api.org/

            Thank you for the info !

            1 Reply Last reply Reply Quote 0
            • D
              daslicht last edited by daslicht

              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 ?

              1 Reply Last reply Reply Quote 0
              • arminunruh
                arminunruh Global Moderator last edited by

                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

                1 Reply Last reply Reply Quote 0
                • D
                  daslicht last edited by

                  Hi,
                  how to I get the category of a post ? please?
                  ~Marc

                  1 Reply Last reply Reply Quote 0
                  • arminunruh
                    arminunruh Global Moderator last edited by

                    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

                    1 Reply Last reply Reply Quote 1
                    • D
                      daslicht last edited by daslicht

                      Thank you !

                      When I try to add RE$ST fields I get:

                      Fatal error: Call to undefined function register_rest_field()
                      

                      Do I need to include something ?
                      Do I need to install the WP-api plugin ?

                      1 Reply Last reply Reply Quote 0
                      • arminunruh
                        arminunruh Global Moderator last edited by

                        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

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Post a link to where the problem is if possible, please <3
                        I don't answer or check forum DMs, please just post on the forum.
                        Forgot your key, lost your files, need a previous Lay Theme or Addon version? Go to www.laykeymanager.com

                        Before you post:

                        Use the Search Feature. Maybe there is already a solution to your issue.

                        1. Update Lay Theme and all Lay Theme Addons
                        2. Disable all Plugins
                        3. Go to Lay Options → Custom CSS & HTML, click "Turn Off All Custom Code ", click "Save Changes"
                        4. When using a WordPress Cache plugin, disable it or clear your cache. Now see if your problem solved itself.
                        Go here, see if your problem is listed here:
                        Troubleshooting

                        When you post:
                        1. Post a link to where the problem is
                        2. If the problem is difficult to explain, post screenshots / link to a video to explain it

                        Thanks!

                        Online Users

                        Recent Topics

                        • Search as icon instead of text?

                        • FILTER Categories showing all projects

                        • H

                          Vimeo embed with autoplay and loop is really slow...help!

                        • A

                          website not loading on iphone safari

                        laytheme.com