Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created April 9, 2021 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/7c64436d32130e11c1b2b60b6d27cfec to your computer and use it in GitHub Desktop.
Save jchristopher/7c64436d32130e11c1b2b60b6d27cfec to your computer and use it in GitHub Desktop.
<?php
// Allow searching by WP_Post ID.
function swp14237_searchwp_results( $results, $attributes ) {
$search_query = get_search_query();
if ( ! is_numeric( $search_query ) ) {
return $results;
}
$the_post = get_post( absint( $search_query ) );
if ( is_null( $the_post ) ) {
return $results;
}
array_unshift( $results, $the_post );
return $results;
}
add_filter( 'searchwp\query\results', 'swp14237_searchwp_results', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment