Remove the post_content search from WHERE clause (and CONCAT sql function)

I've a custom table and by following the instructions on this page https://codex.wordpress.org/Custom_Queries I was able to modify the WHERE clause.

function geotag_search_where( $where ){
   if( is_search() ) {
         $where = preg_replace( "/\(\s*" . $wpdb-posts . ".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
         " CONCAT( posts.post_title, customtable.customfield) LIKE $1 ", $where );
      }

   return $where;
}

add_filter('posts_where', 'geotag_search_where' );

My only problem is that wordpress keeps the OR (posts.post_content LIKE '%searchterm%') and this slows down my search time. Is there a way to modify the preg_replace() so that it will exclude also the post_content? Thanks!

EDIT [as per @Prosti request]

Here is how is now my WHERE clause

SELECT [normal select from worpress] 
 ….
[customized from my filters]
LEFT JOIN customtable ON FIND_IN_SET(posts.ID, customtable.postID) 0 
WHERE 1=1 
AND (term_relationships.term_taxonomy_id IN (7) ) 
AND (( CONCAT(posts.post_title, customtable.customfield) LIKE '%searchterm%' 
    OR (posts.post_content LIKE '%searchterm%'))) 
[end of customization]
 ……. 
AND [rest is normal from worpress]

I would like to exclude the OR (posts.post_content LIKE '%searchterm%')

so that sql would be:

....
AND (( CONCAT(posts.post_title, customtable.customfield) LIKE '%searchterm%' ))
....

Topic posts-where filters Wordpress search

Category Web


First look how complex is your preg replace. Usually, you may get into errors with that.

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  /                        '/'
--------------------------------------------------------------------------------
  \(                       '('
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  post_title               'post_title'
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  LIKE                     'LIKE'
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \'                       '''
--------------------------------------------------------------------------------
    [^\']+                   any character except: '\'' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \'                       '''
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \)                       ')'
--------------------------------------------------------------------------------
  /",       "              '/",       "'
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    post_title LIKE          'post_title LIKE '
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
    1                        '1'
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
   OR                      ' OR '
--------------------------------------------------------------------------------
  (                        group and capture to \3:
--------------------------------------------------------------------------------
    geotag_city LIKE         'geotag_city LIKE '
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
    1                        '1'
--------------------------------------------------------------------------------
  )                        end of \3
--------------------------------------------------------------------------------
   OR                      ' OR '
--------------------------------------------------------------------------------
  (                        group and capture to \4:
--------------------------------------------------------------------------------
    geotag_state LIKE        'geotag_state LIKE '
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
    1                        '1'
--------------------------------------------------------------------------------
  )                        end of \4
--------------------------------------------------------------------------------
                OR         '              OR '
--------------------------------------------------------------------------------
  (                        group and capture to \5:
--------------------------------------------------------------------------------
    geotag_country           'geotag_country LIKE '
    LIKE
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
    1                        '1'
--------------------------------------------------------------------------------
  )                        end of \5

Next, you need to provide full SQL query example after preg_replace .

If we can have the full SQL query, we may also understand the customization.

Looking at what you wrote we need to have the arguments you set for the WP_Query, to understand how you organized the query.

More, CONCAT is not a smart thing in there, it forces MySQL to concatenate strings — a slow down factor.

Hope this may be helpful.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.