Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Integrate SearchWP and Nexus ElegantTheme
<?php
// This code is ideally added to a child theme's functions.php
// OR
// at the *bottom* of the main theme's functions.php
// (do not forget to remove the opening <?php tag from this gist)
if( is_search() && class_exists( 'SearchWP' ) ) {
// remove the troublesome theme filter
remove_action( 'pre_get_posts', 'et_custom_posts_per_page' );
// replicate the functionality
if ( isset( $_GET['et-month-choice'] ) && $_GET['et-month-choice'] != 'no-choice' ) {
add_filter( 'searchwp_include', 'my_searchwp_limit_by_date', 10, 3 );
}
if ( isset( $_GET['et-cat'] ) && $_GET['et-cat'] != 0 ) {
add_filter( 'searchwp_include', 'my_searchwp_include_only_category', 10, 3 );
}
}
if( ! function_exists( 'my_searchwp_include_only_category' ) ) {
function my_searchwp_include_only_category( $ids, $engine, $terms ) {
if( isset( $_GET['et-cat'] ) ) {
$categoryID = absint( $_GET['et-cat'] );
$args = array( 'category' => $categoryID, 'fields' => 'ids' );
$limited_ids = get_posts( $args );
if( ! empty( $limited_ids ) ) {
$ids = array_merge( $ids, $limited_ids );
$ids = array_unique( $ids );
}
}
return $ids;
}
}
if( ! function_exists( 'my_searchwp_limit_by_date' ) ) {
function my_searchwp_limit_by_date( $ids, $engine, $terms ) {
if ( isset( $_GET['et-month-choice'] ) && $_GET['et-month-choice'] != 'no-choice' ) {
$et_year = absint( substr($_GET['et-month-choice'],0,4) );
$et_month = absint( substr($_GET['et-month-choice'], 4, strlen($_GET['et-month-choice'])-4) );
$args = array( 'year' => $et_year, 'monthnum' => $et_month, 'fields' => 'ids' );
$limited_ids = get_posts( $args );
if( ! empty( $limited_ids ) ) {
$ids = array_merge( $ids, $limited_ids );
$ids = array_unique( $ids );
}
}
return $ids;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.