Last active
December 31, 2015 01:09
-
-
Save aliciaduffy/7912286 to your computer and use it in GitHub Desktop.
WordPress query two categories at once - ex. http://website.com/?category_name=cats+dogs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function duffy_category_search_logic( $query ) { | |
if ( !isset( $query->query_vars[ 'category_name' ] ) ) | |
return $query; | |
// split cat query on a space to get IDs separated by '+' in URL | |
$cats = explode( ' ', $query->query_vars[ 'category_name' ] ); | |
if ( count( $cats ) > 1 ) { | |
unset( $query->query_vars[ 'category_name' ] ); | |
$query->query_vars[ 'category__and' ] = $cats; | |
} | |
return $query; | |
} | |
add_action( 'parse_request', 'duffy_category_search_logic', 11 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form role="search" class="searchform custom" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>"> | |
<input type="hidden" name="category_name" value="cats dogs" /> | |
<input type="text" name="s" id="s" class="s" placeholder="Search" /> | |
<input class="button" type="submit" id="searchsubmit" value="Submit" /> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment