search ALSO by POST-ID with a public front-end search box

Question

I already have a code letting me start a search (also) by post ID, using the public front-end search box. I’d like to search ONLY in the content/id of post-type=post, now the search is not limited and is over all type of content, also pages, custom posts and so on, how I could fix the following function?

//cerca per ID
add_filter('posts_where', 'posts_where');
function posts_where($where)
    {

        $s = $_GET['s'];

        if(!empty($s))
        {
          if(is_numeric($s))
          {
             global $wpdb;

            $where = str_replace('(' . $wpdb->posts . '.post_title LIKE', '(' . $wpdb->posts . '.ID = ' . $s . ') OR (' . $wpdb->posts . '.post_title LIKE', $where);
          }
          elseif(preg_match("/^(\d+)(,\s*\d+)*\$/", $s)) // string of post IDs
          {
            global $wpdb;

            $where = str_replace('(' . $wpdb->posts . '.post_title LIKE', '(' . $wpdb->posts . '.ID in (' . $s . ')) OR (' . $wpdb->posts . '.post_title LIKE', $where);
          }
        }


      return $where;
    }

0
Andrea Sacconi 4 weeks 2023-05-07T04:45:19-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse