Skip to content
  • Recent
  • Tags
  • Popular
  • Users
  • Search
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

Lay Theme Forum

  1. Home
  2. General Discussion
  3. Custom Queries

Custom Queries

Scheduled Pinned Locked Moved General Discussion
10 Posts 2 Posters 1.9k Views 2 Watching
  • 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
    D
    daslicht
    wrote on last edited by daslicht
    #1

    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
    0
    • D 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
      D
      daslicht
      wrote on last edited by daslicht
      #2

      Selecting by id works :

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

        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
        1
        • D
          D
          daslicht
          wrote on last edited by
          #4

          @arminunruh said:

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

          Thank you for the info !

          1 Reply Last reply
          0
          • D
            D
            daslicht
            wrote on last edited by daslicht
            #5

            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
            0
            • arminunruhA
              arminunruhA
              arminunruh
              Global Moderator
              wrote on last edited by
              #6

              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
              0
              • D
                D
                daslicht
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • arminunruhA
                  arminunruhA
                  arminunruh
                  Global Moderator
                  wrote on last edited by
                  #8

                  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
                  1
                  • D
                    D
                    daslicht
                    wrote on last edited by daslicht
                    #9

                    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
                    0
                    • arminunruhA
                      arminunruhA
                      arminunruh
                      Global Moderator
                      wrote on last edited by
                      #10

                      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
                      0

                      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
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      Online Users
                      A
                      Anna_S
                      Forgot your key, lost your files, need a previous Lay Theme or Addon version? Go to www.laykeymanager.com
                      laytheme.com
                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Search