• Resolved pedjas

    (@pedjas)


    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

    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 https://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:

    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.

    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?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter pedjas

    (@pedjas)

    I tried to add this:

    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 1 year, 4 months ago by pedjas.
    • This reply was modified 1 year, 4 months ago by pedjas.
    Thread Starter pedjas

    (@pedjas)

    Solved it by manually setting filter in pre_get_posts.

    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 1 year, 4 months ago by pedjas.
    Moderator bcworkz

    (@bcworkz)

    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

    (@pedjas)

    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

    (@bcworkz)

    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. 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.