Title: Cannot apply category from URL to QueryLoop Block
Last modified: July 12, 2023

---

# Cannot apply category from URL to QueryLoop Block

 *  Resolved [pedjas](https://wordpress.org/support/users/pedjas/)
 * (@pedjas)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/cannot-apply-category-from-url-to-queryloop-block/)
 * I have custom post type named ‘site’. I created Page ‘sites’ that contains QueryLoop
   set to show all posts of type ‘site’. That works fine. I can open page at “www.
   mydomain.com/sites/” and I see all posts.
 * _I tried same thing using custom archive first, but with no result (the same 
   behavior) so I switched to try with page._
 * Custom Post type uses Categories so I want to be able to show posts filtered 
   by category. Idea is to use url to specify category like: ‘www.mydomain.com/sites/
   sports’.
   I created
 *     ```wp-block-code
       add_action( 'init', 'un_add_sites_rewrite_rule');
   
       function un_add_sites_rewrite_rule() {
               $rule =  '^sites/(.+?)/?$';
               $rewrite = 'index.php?pagename=sites&category_name=$matches[1]';
   
               add_rewrite_rule($rule,$rewrite,'top');
   
       };
       ```
   
 * After that, url [http://www.mydomain.com/sites/sports](http://www.mydomain.com/sites/sports)
   works (no 404 error) but shows all posts, not filtered by category.
 * For some reason query_var category_name is not taken in consideration by QueryLoop.
   From what I learned so far QueryLoop should use category_name value if it is 
   set.
 * I created action to investigate what happens on query:
 *     ```wp-block-code
       add_action( 'pre_get_posts', 'un_test_posts_query' );
   
       function un_test_posts_query( $query ) {
         if ($query->query['post_type'] == 'site') {
           echo "<pre>";
           global $wp;
           print_r ($wp->query_vars);
           print_r ($query->query)
           echo "<pre>";
       }
       ```
   
 * This shows that var_query does contain category_name and it is properly populated
   as expected. But $query does not follow that.
 *     ```wp-block-code
       Array
       (
           [category_name] => sports
           [pagename] => sites
       )
       Array
       (
           [post_type] => site
           [order] => ASC
           [orderby] => title
           [post__not_in] => Array
               (
               )
   
           [offset] => 0
           [posts_per_page] => 24
           [tax_query] => Array
               (
               )
   
           [meta_query] => Array
               (
               )
   
       )
       ```
   
 * I am confused. Why this does not work?
    -  This topic was modified 2 years, 9 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).

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

 *  Thread Starter [pedjas](https://wordpress.org/support/users/pedjas/)
 * (@pedjas)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/cannot-apply-category-from-url-to-queryloop-block/#post-16890327)
 * I tried to add this:
 *     ```wp-block-code
       add_filter( 'query_loop_block_query_vars', 'un_filter_sites_query' );
   
       function un_filter_sites_query ($query ) {
         global $wp;
   
         if ($query->query['post_type'] == 'site') {
           $query['category_name'] == $wp->query_vars['category_name'];
         }
   
         return $query;
   
       }
       ```
   
 * No change. It still shows all posts unfiltered.
    -  This reply was modified 2 years, 9 months ago by [pedjas](https://wordpress.org/support/users/pedjas/).
    -  This reply was modified 2 years, 9 months ago by [pedjas](https://wordpress.org/support/users/pedjas/).
 *  Thread Starter [pedjas](https://wordpress.org/support/users/pedjas/)
 * (@pedjas)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/cannot-apply-category-from-url-to-queryloop-block/#post-16890389)
 * Solved it by manually setting filter in pre_get_posts.
 *     ```wp-block-code
       add_action( 'pre_get_posts', 'un_test_posts_query' );
   
       function un_test_posts_query( $query ) {
         global $wp;
   
         if ($query->query['post_type'] == 'site') {
           $query->set ('category_name', $wp->query_vars['category_name']);
   
         }
   
         return $query;
   
       }
       ```
   
 * I still not understand why query does not gather this automatically.
    -  This reply was modified 2 years, 9 months ago by [pedjas](https://wordpress.org/support/users/pedjas/).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/cannot-apply-category-from-url-to-queryloop-block/#post-16890632)
 * You’ve set the query loop block to not inherit from the template, correct?
 * The block’s query code is meant to use the category specified in its filter block
   settings, not whatever is passed as a query string. Even though the query var
   appears in `$wp`, it does not mean it’ll be passed to the block’s query because
   query strings are normally assigned to the main query (page “sites” in your case),
   not a secondary block query.
 * You had the right idea in your first reply, but you assigned the passed value
   to the wrong property. Should have been `$query->query_vars['category_name'] 
   == $wp->query_vars['category_name'];` By using $query->set() method instead, 
   it ensures the value is assigned correctly.
 *  Thread Starter [pedjas](https://wordpress.org/support/users/pedjas/)
 * (@pedjas)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/cannot-apply-category-from-url-to-queryloop-block/#post-16891539)
 * I first tried using $query->set() but in query_loop_block_query_vars it is not
   available. 
   It worked in pre_get_posts.It would be helpful, that QueryLoop block
   has options for user to set values for specific properties by using existing 
   global variables or from template.For example, option not just to select categories
   to filter by category, but to set it filter by global_var category_name, or so.
 * QueryLoop is generally lacking of options to dynamically use some data from the
   system. All settings must be entered manually and stay fixed.
 * Yes, I am talking about usage when QueryLoop is set not to inherit from the template
   which is used whenever something needs to be customized. User has just two options:
   either to completely relay on collecting all from template or having to set all
   manually and fixed. There should be option to mix two of them: some QueryLoop
   properties needs to be collected from template, and some needs to be set fixed,
   or even override values from template.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/cannot-apply-category-from-url-to-queryloop-block/#post-16893471)
 * A balance needs to be struck between offering useful options and overwhelming
   users with options they don’t have any use for. It’s common to relegate less 
   common options to be managed by plugins and custom code.
 * I do like the idea of using template configuration but optionally override a 
   few things. If you’d like to make a formal suggestion, raise an enhancement issue
   at the [Project Gutenberg GitHub site](https://github.com/WordPress/gutenberg/issues).
   Please do a search first to be sure your enhancement has not already been suggested.

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

The topic ‘Cannot apply category from URL to QueryLoop Block’ is closed to new replies.

## Tags

 * [add_rewrite_rule()](https://wordpress.org/support/topic-tag/add_rewrite_rule/)
 * [category_name](https://wordpress.org/support/topic-tag/category_name/)
 * [query_vars](https://wordpress.org/support/topic-tag/query_vars/)
 * [wpquery](https://wordpress.org/support/topic-tag/wpquery/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [2 years, 9 months ago](https://wordpress.org/support/topic/cannot-apply-category-from-url-to-queryloop-block/#post-16893471)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
