Display Custom FacetWP Query Args in Search Results Header

In a recent project I was tasked with creating a fairly complex search results page using FacetWP. Creating the search results template was straight forward and integrating FacetWP is always a breeze. The complex part was the need for links throughout the site to automatically query the appropriate posts while displaying that query in the “Search Results For:” area.

As an example use case, on this specific site we are not using category archive pages and the client wanted all category links to go directly to the search results page with the category pre-selected. So category links needed to include the proper query_var, which would need to be called upon using the method below to output the proper text. FacetWP makes it super simple to pre-select the correct Facet options, but getting that text to show is a different story.

Set Search Query & Query Vars as Variables

The first step is to get the possible query_var arguments from the URL and set them as variables that can be used later. To do that we want to use get_query_var. Additionally, we’ll set an empty variable for the query output and set the normal search query text as a variable (which I’ll explain later). This is all done directly in the search.php template in my case.

<?php // don't include opening tag

$query_output = '';
$search_query = get_search_query();
$category_qa = get_query_var( '_categories' );
$post_type_qa = get_query_var( '_post_type' );
$author_qa = get_query_var( '_author' ); // gets author ID

// set query output based on emtpy search
// if search is empty check facetWP query vars
if( empty( $search_query ) ) {

     if( $category_qa ) {
          $query_output .= $category_qa . ' ';
     }

     if( $post_type_qa ) {
          $query_output .= $post_type_qa . ' ';
     }

     if( $author_qa ) {
          $author_nicename = get_the_author_meta( 'display_name', $author_qa ); // get author's display name;
          $query_output .= $author_nicename;
     }

} else {
     $query_output .= $search_query;
}
?>

In the above snippet you’ll see our empty $query_output variable followed by a set of variables for each possible option:

  • A normal search query ($search_query)
  • Custom query vars (in this case custom Facets – $category_qa, $post_type_qa and $author_qa)

The custom query vars should match how the query is output in the URL. So, for example, the $category_qa is looking for _categories in the URL to set the appropriate variable.

We then check to see if there is a normal search query. If there is, we set the $query_output variable as the $search_query. If there is not, we check each possible query vars and append that variable output to the $query_output variable. This gives us the proper search query set at a variable.

Keep in mind that there may be more than one query var, so we need to append to the variable and not overwrite it. For example, there could be a link that is looking for a specific category in a specific custom post type.

Register Custom Query Vars

One issue, before we can move forward, is that get_query_var won’t work with custom query arguments that aren’t registered with WordPress. To ensure we can use get_query_var we need to register each custom query arg as follows:

<?php // don't include opening php tag

// register custom query args to allow use of get_query_var in search template
add_action( 'init', 'add_get_val' );
function add_get_val() {
     global $wp;
     $wp->add_query_var( '_categories' );
     $wp->add_query_var( '_post_type' );
     $wp->add_query_var( '_author' );
}

This should be done in a functionality plugin or your functions.php file.

Output Custom Search or Query Vars

We now have variables set for each possible query arg that we can use to output as needed. Here is the full snippet showing how we output the content:

<?php
add_action( 'genesis_after_header', 'wd_genesis_do_search_title' );
function wd_genesis_do_search_title() { ?>

     <?php
     $query_output = '';
     $search_query = get_search_query();
     $category_qa = get_query_var( '_categories' );
     $post_type_qa = get_query_var( '_post_type' );
     $author_qa = get_query_var( '_author' );

     // set query output based on emtpy search
     // if search is empty check facetWP query vars
     if( empty( $search_query ) ) {

          if( $category_qa ) {
               $query_output .= $category_qa . ' ';
          }

          if( $post_type_qa ) {
               $query_output .= $post_type_qa . ' ';
          }

          if( $author_qa ) {
               $author_nicename = get_the_author_meta( 'display_name', $author_qa );
               $query_output .= $author_nicename;
          }

     } else {
          $query_output .= $search_query;
     }
     ?>

     <div class="search-header">
          <div class="wrap">

               <div class="search-header__left">
                    <span class="search-header__leader">Search Results For:</span>
                    <h2><?php echo $query_output; ?></h2>
               </div>

               <div class="search-header__results">
                    <?php echo facetwp_display( 'counts' ); ?>
               </div>

          </div>
     </div>

<?php }

You’ll see I am using genesis_after_header to place this content above the entry content, just after the header output. Then within the output we echo the $query_output to ensure the associated search query or the appropriate query argument displays when a user gets to the search results page.

If you aren’t using Genesis or would like to output your search query in a different location, you can simply change what action you are hooking to.

Downside to this method

The main downside to this method is that the search query won’t change if the user changes the filters (Facets) on the search results page. Typically, you can’t change your search results, but in this case we are using FacetWP on the search results page. So when the user does change the filters, the text displaying in the “Search Results for:” area don’t change.

Leave a Reply

Your email address will not be published. Required fields are marked *