Occasionally, I get asked if it is possible to set a “default category” when displaying the entry list. The simple answer is yes; use the category shortcode option. But, the answer can be a little more complex than that. If you are using any of the templates which allow the user to filter the results by category and keyword search when adding the category shortcode option, you are also limiting the search to that category unless the user first switches to a different category using the category dropdown. The search is then limited to the newly selected category. This feature is by design. This is to allow a user to narrow their search to within their chosen category. This works in much the same way when searching for products at Amazon and choose to limit the search to a “department” (basically another term for category).
Most users are very satisfied with being able to set a default category and have search work in this way; however, this does not always meet everyone’s requirements. Some need the search to search the entire directory whether or not the “default category” is set using the category shortcode option. This is possible but it has not been thoroughly tested; however, those that are using a code snippet (a filter) have reported it suited their needs perfectly. Here’s how to apply a filter to override the default search behavior.
First, you must install the Code Snippets plugin. This plugin is a fantastic tool that can be used to add update-safe WordPress actions and filters. When searching howtos on the web you will very often come across a tutorial that says something like add this to your theme functions.php file. You are far better served by using this plugin instead, as it makes managing these code snippets far easier, and you are much less likely to break your site.
After you have installed the Code Snippets plugin, add a new snippet with the following code:
function cn_reset_category_search_filter( $posts, $WP_Query ) { global $wp_query; $shortcode = 'connections'; $pattern = get_shortcode_regex(); // Grab the array containing all query vars registered by Connections. $registeredQueryVars = cnRewrite::queryVars( array() ); foreach ( $posts as $post ) { // If we're in the main query, proceed! if ( isset( $WP_Query->queried_object_id ) && $WP_Query->queried_object_id == $post->ID ) { /* * $matches[0] == An array of all shortcode that were found with its options. * $matches[1] == Unknown. * $matches[2] == An array of all shortcode tags that were found. * $matches[3] == An array of the shortcode options that were found. * $matches[4] == Unknown. * $matches[5] == Unknown. * $matches[6] == Unknown. */ if ( preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches ) && array_key_exists( 2, $matches ) && in_array( $shortcode, $matches[2] ) ) { // Parse the shortcode atts. $atts = shortcode_parse_atts( $matches[3][ array_search( $shortcode, $matches[2] ) ] ); // Remove the cn-image query vars. $wp_query->query_vars = array_diff_key( (array) $wp_query->query_vars, array_flip( array( 'src', 'w', 'h', 'q', 'a', 'zc', 'f', 's', 'o', 'cc', 'ct' ) ) ); // If the `category` shortcode option is set and the user is not performing a search of any type, limit the results the categories defined by the shorcode option. // If a Connections query var is set, show all results according to the query. if ( isset( $atts['category'] ) && (bool) array_intersect( $registeredQueryVars, array_keys( (array) $wp_query->query_vars ) ) ) { unset( $atts['category'] ); // Rewrite the $atts array to prep it to be imploded. array_walk( $atts, create_function( '&$i,$k','$i="$k=\"$i\"";' ) ); $replace = '[' . $shortcode . ' ' . implode( ' ', $atts ) . ']'; } else { // Rewrite the $atts array to prep it to be imploded. array_walk( $atts, create_function( '&$i,$k','$i="$k=\"$i\"";' ) ); $replace = '[' . $shortcode . ' ' . implode( ' ', $atts ) . ']'; } // Replace the shortcode in the post with something a new one based on you changes to $atts. $post->post_content = str_replace( $matches[0][ array_search( $shortcode, $matches[2] ) ], $replace, $post->post_content ); } } } return $posts; } add_filter( 'the_posts', 'cn_reset_category_search_filter', 1000, 2 );
Lastly, save and activate this new snippet.
That should be all… now, when a keyword search is being performed by the user, this snippet will check for that and if the category shortcode option is set, it will dynamically remove it, permitting the search of the entire directory.
Disclaimer: This is not supported, nor has it been thoroughly tested, so your results may vary.