Title: Query by URL variable?
Last modified: February 19, 2021

---

# Query by URL variable?

 *  [LesTexas60](https://wordpress.org/support/users/lestexas60/)
 * (@lestexas60)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/query-by-url-variable/)
 * I love the concept of this plugin because it makes it so easy to filter posts.
   That being said, it would be great if a URL variable could define the category
   that was going to be filtered. Simple Buttons with the URL and variable could
   define the category to display in the Post display widget. Just a suggestion.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [LesTexas60](https://wordpress.org/support/users/lestexas60/)
 * (@lestexas60)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/query-by-url-variable/#post-14076854)
 * Solved: Snippet with a little php code to create a Query ID that uses Get to 
   pull in the variable value. My button refreshes the page and sets the category
   value.
 *  [trojan4usc](https://wordpress.org/support/users/trojan4usc/)
 * (@trojan4usc)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/query-by-url-variable/#post-14181447)
 * Hi Les, I have been trying to do the same. In my case I have pages that are dynamically
   generated, and include a number of URL variables. One of those variables the 
   the category that the Posts Widget should use to filter out my posts.
 * Elementor does not support this capability natively, so I was wondering how your
   code snippet looks to set the category for the posts widget. Can you please share
   what you came up with?
 * Regards,
    Dan
 *  Thread Starter [LesTexas60](https://wordpress.org/support/users/lestexas60/)
 * (@lestexas60)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/query-by-url-variable/#post-14181485)
 * Hi Dan, Here is the code I use in the code snippet. The name Leslie2 is the Query
   ID in the post display widget. The variable is animal.
 * Go to this page and select an animal and you will go to a Dynamic created template
   that just changes to show the different animal groups. 1 page shows all the different
   animal groups depending on the URL variable value that was set in the link.
 * [https://tigercat721.pawsinthewoodlands.com/querybase/](https://tigercat721.pawsinthewoodlands.com/querybase/)
 *     ```
       // Showing multiple post types in Posts Widget
   
       add_action( 'elementor/query/leslie2', function( $query ) {
       	// Here we set the query to fetch posts with
       	// post type of 'custom-post-type1' and 'custom-post-type2'
       	$c =  htmlspecialchars($_GET["animal"]);
         if ( $c == "dogs" ) {
       		 $query->set( 'category_name' , 'dogs'); 
         }
         if ( $c == "cats" ) {
       		 $query->set( 'category_name' , 'cats'); 	
       }
       	 if ( $c == "turtles" ) {
       		 $query->set( 'category_name' , 'turtles'); 	
       }
       	 if ( $c == "lizards" ) {
       		 $query->set( 'category_name' , 'lizards'); 	
       }
       	 if ( $c == "monkey-tree" ) {
       		 $query->set( 'category_name' , 'monkey-tree'); 	
       }	
   
       } );
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Query by URL variable?’ is closed to new replies.

 * ![](https://ps.w.org/advanced-post-queries/assets/icon-128x128.png?rev=2319579)
 * [Advanced Queries](https://wordpress.org/plugins/advanced-post-queries/)
 * [Support Threads](https://wordpress.org/support/plugin/advanced-post-queries/)
 * [Active Topics](https://wordpress.org/support/plugin/advanced-post-queries/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/advanced-post-queries/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/advanced-post-queries/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [LesTexas60](https://wordpress.org/support/users/lestexas60/)
 * Last activity: [5 years, 2 months ago](https://wordpress.org/support/topic/query-by-url-variable/#post-14181485)
 * Status: not resolved