Skip to content

Instantly share code, notes, and snippets.

@Spellhammer
Last active October 9, 2024 20:10
Show Gist options
  • Save Spellhammer/892d37792501325325bea2920d991193 to your computer and use it in GitHub Desktop.
Save Spellhammer/892d37792501325325bea2920d991193 to your computer and use it in GitHub Desktop.
Search Results Not Found Condition
if( function_exists('oxygen_vsb_register_condition') ) {
global $oxy_condition_operators;
oxygen_vsb_register_condition('Has Results', array('options'=>array('true', 'false'), 'custom'=>false), array('=='), 'search_has_results_callback', 'Search');
function search_has_results_callback($value, $operator) {
global $wp_query;
$posts_found = $wp_query->found_posts;
if( $value == "true" && $posts_found > 0) {
return true;
} else if( $value == "false" && $posts_found == 0 ) {
return true;
}
}
}
@Chosebow
Copy link

Hello, and thanks for the code! I have a little problem. The results page is returning images, as well as blog posts and pages, but if instead of a default query, I use a custom query and specify the post type as post only, the search result is all blog posts. Any ideas? Thank you!

@Digitalarbeit
Copy link

HI @Chosebow
i solve this problem with another Code Snippet to exclude some pages

function wpsnippets_exclude_pages_from_search( $query ) {
    if ( ! is_admin() && $query->is_search() ) {
        // Array of page IDs to exclude from search
        $exclude_pages = array( 2950, 2750);
        $query->set( 'post__not_in', $exclude_pages );
    }
}
add_action( 'pre_get_posts', 'wpsnippets_exclude_pages_from_search' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment