search posts by POST ID

The function you are using might not be working because WordPress’s search functionality has been updated, and it’s recommended to use WordPress’s WP_Query class to modify the main query. Here’s a better and safer way to search by post ID using pre_get_posts hook:

function search_by_post_id($query) {
    if($query->is_search) {
        if(is_numeric($query->query_vars['s'])) {
            $query->set('post_type', 'any');
            $query->set('post__in', array((int)$query->query_vars['s']));
            $query->set('s', '');
    return $query;
add_filter('pre_get_posts', 'search_by_post_id');

When a search is performed, the function checks if the search term is numeric. If it is, it interprets the term as a post ID and modifies the query to fetch a post with that ID. It sets the post type to ‘any‘ to make sure it can find any type of post, not just regular ‘posts’. This should work for any numerical ID entered into the search bar.