-
-
Save searchwpgists/ff63730081ec4eda7142d1981c07b976 to your computer and use it in GitHub Desktop.
Limit SearchWP results to chosen Category from dropdown
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 | |
// Limit SearchWP results to chosen Category from dropdown. | |
// @link https://searchwp.com/documentation/knowledge-base/category-select-dropdown/ | |
add_filter( 'searchwp\query\mods', function( $mods, $query ) { | |
global $wpdb; | |
// Only proceed if a Category was chosen from the dropdown. | |
if ( ! isset( $_GET['swp_category_limiter'] ) || empty( intval( $_GET['swp_category_limiter'] ) ) ) { | |
return $mods; | |
} | |
// Optional: only proceed if we're using a specific Engine. | |
// if ( 'default' !== $query->get_engine()->get_name() ) { | |
// return $mods; | |
// } | |
$alias = 'swpkbcat'; | |
$tax_query = new WP_Tax_Query( [ [ | |
'taxonomy' => 'category', | |
'field' => 'term_id', | |
'terms' => absint( $_GET['swp_category_limiter'] ), | |
] ] ); | |
$tq_sql = $tax_query->get_sql( $alias, 'ID' ); | |
$mod = new \SearchWP\Mod(); | |
// If the JOIN is empty, WP_Tax_Query assumes we have a JOIN with wp_posts, so let's make that. | |
if ( ! empty( $tq_sql['join'] ) ) { | |
// Queue the assumed wp_posts JOIN using our alias. | |
$mod->raw_join_sql( function( $runtime ) use ( $wpdb, $alias ) { | |
return "LEFT JOIN {$wpdb->posts} {$alias} ON {$alias}.ID = {$runtime->get_foreign_alias()}.id"; | |
} ); | |
// Queue the WP_Tax_Query JOIN which already has our alias. | |
$mod->raw_join_sql( $tq_sql['join'] ); | |
// Queue the WP_Tax_Query WHERE which already has our alias. | |
$mod->raw_where_sql( '1=1 ' . $tq_sql['where'] ); | |
} else { | |
// There's no JOIN here because WP_Tax_Query assumes a JOIN with wp_posts already | |
// exists. We need to rebuild the tax_query SQL to use a functioning alias. The Mod | |
// will ensure the JOIN, and we can use that Mod's alias to rebuild our tax_query. | |
$mod->set_local_table( $wpdb->posts ); | |
$mod->on( 'ID', [ 'column' => 'id' ] ); | |
$mod->raw_where_sql( function( $runtime ) use ( $tax_query ) { | |
$tq_sql = $tax_query->get_sql( $runtime->get_local_table_alias(), 'ID' ); | |
return '1=1 ' . $tq_sql['where']; | |
} ); | |
} | |
$mods[] = $mod; | |
return $mods; | |
}, 20, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment