Skip to content
  • Recent
  • Tags
  • Popular
  • Users
  • Search
Skins
  • Light
  • 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.1k Views
  • 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 Offline
    D Offline
    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 Offline
      D Offline
      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 Offline
        arminunruhA Offline
        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 Offline
          D Offline
          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 Offline
            D Offline
            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 Offline
              arminunruhA Offline
              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 Offline
                D Offline
                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 Offline
                  arminunruhA Offline
                  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 Offline
                    D Offline
                    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 Offline
                      arminunruhA Offline
                      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
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      I also code custom websites or custom Lay features.
                      šŸ’æ Email me here: šŸ’æ
                      info@laytheme.com

                      Before you post:
                      1. When using a WordPress Cache plugin, disable it or clear your cache.
                      2. Update Lay Theme and all Lay Theme Addons
                      3. Disable all Plugins
                      4. 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:
                      1. Post a link to where the problem is
                      2. Does the problem happen on Chrome, Firefox, Safari or iPhone or Android?
                      3. If the problem is difficult to explain, post screenshots / link to a video to explain it
                      Online Users
                      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