NO BUG: Query loops in header/footer templates don't output correctly on taxonomy archive pages

Bricks Version: 1.6.2
Browser: Multiple
OS: macOS

Hi gang.

Query loops placed in a header or footer template break when being rendered on taxonomy archive template pages.

For example, a footer with multiple query loops outputting all posts for a handful of CPTs (i.e. services, products, etc.) will be blank when viewed on a taxonomy archive page.

Note that those same footer query loops work fine on post archive pages. This just seems to be an issue with taxonomy archives.

1 Like

Hi @foliot ,

If you are using Query Loop in header or footer template, the query parameters will be auto-merge with WP main queries in archive pages by Bricks.

You could exclude the merge for that particular query loop by using this filter:

Please let me know if this works.

By the way, the dev team will add a checkbox in the Query loop UI so you could exclude the merge without the PHP filter.

Regards,
Jenn

2 Likes

Hi @ itchycode,

That absolutely solved my issue.

Thanks for the fix! I appreciate it.

1 Like

Hi

in case somebody wants to see a “use case” for this filter to link the search query from the Bricks Search widget together with WPGridbuilder Facets on the Result Template Page.

What I did:

First, I added the filter into a WPCodeBox snippet:

<?php 
add_filter( 'bricks/posts/merge_query', function( $merge, $element_id ) {
  if ( $element_id === 'gfpkfh' ) {
    return false;
  }

  return $merge;
}, 10, 2 );

The element ID ‘gfpkfh’ is from my query and layout, that I made in the Bricks Search Template:

Search Results9

‘gfpkfh’ is the Container element under everything else is included; all the facets, the query and the layouts.

The WPGridbuilder Facets under this main Container element ‘gfpkfh’ all have my query block in the “Select a grid or element to filter”, in my case it is #iblqce.

Search Results10

My query #iblqce:

Search Results11

In my query, I defined that I only want to see results that are in the custom post type “Rezepte” and I also defined the sorting:

Search Results13

These query settings will be “merged” with the query settings that come from the Search widget.

That’s it. When I search for something via the Bricks search widget in the header, the Bricks Search Results template page is shown with my own query, my layout and the WPGridbuilder facets:

So with this filter it is possible to link the WPGridbuilder Facets and a custom query with the Search Results Template.

Cheers

Patric

2 Likes

Update. If you have a WPGridbuilder Search facet in your Bricks search results page and want that this Search facet shows the terms / words / whatever the user entered in the Bricks search form widget to look for when the results page is loaded:

Then you can use this code (for example add it into a WPCodeBox snippet):

 <?php 
function prefix_filter_query( $query_string, $grid_id, $action ) {

	// If we are on the search results page and a search parameter exists
	if ( 'render' === $action && is_search() && isset($_GET['s']) ) {
		
	$suchkey = htmlspecialchars($_GET["s"]);
	$suchkey = sanitize_text_field( $suchkey );
	        if (!empty($suchkey)) { 
			$query_string = [
			'rezept_stichwortsuche_2'   => [ $suchkey ]
			         		];
       }    
       }
      return $query_string;
}
add_filter( 'wp_grid_builder/facet/query_string', 'prefix_filter_query', 10, 3 );

This code is using the very powerful “prefix_filter_query” function from WPGridbuilder.

Just replace the “rezept_stichwortsuche_2” with the facet slug from the “Naming” setting in the WPGridbuilder facet settings:

Note: I made this check in the code

if ( 'render' === $action && is_search() && isset($_GET['s']) ) {

so that the Search facet is only filled with the search terms if the page is a search page and if there is a search parameter (=“?s”) in the url of the page. This makes sure that only the Search facet on the search results page is getting updated.

You can of course change that code line.

Cheers

Patric

2 Likes

Thank you so much :metal:

I needed this… Works like charm - also with an array.

add_filter( 'bricks/posts/merge_query', function( $merge, $element_id ) {
  $target_ids = [
	  'fe017a', 
	  'eaaeef', 
	  '0ba2b2', 
	  'ybezza'
  ];
  if ( in_array($element_id, $target_ids) ) {
    return false;
  }

  return $merge;
}, 10, 2 );