Skip to main content
Better formatting
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

When you do this:

$querystr = "SELECT * FROM `wp_posts`  
WHERE post_content 
    LIKE '%Alba%' 
    AND post_type='post'  
OR 
    LOWER(post_title) 
    LIKE '%$nume_searchq%'  
OR 
    LOWER(post_content) 
    LIKE '%$spec_searchq%'  
OR 
    LOWER(post_content) 
    LIKE '%$instit_searchq%'  
LIMIT $limit ";

If ANY of the variables are blank ($nume_searchq || $spec_searchq || $instit_searchq$nume_searchq || $spec_searchq || $instit_searchq ), you will get all posts, because "LIKE '%%'" LIKE '%%' matches everything.

So this leaves you with a couple of options. 1) Explicitly set each of those variables every time --or-- 2) Conditionally include them depending on if they contain something valuable.

  1. Explicitly set each of those variables every time --or--

  2. Conditionally include them depending on if they contain something valuable.

For example:

case "AB":
    $querystr = "SELECT * FROM `wp_posts` WHERE post_content LIKE '%Alba%' AND post_type='post'";

    if ( isset( $nume_searchq ) && $nume_searchq !== '' )
        $querystr .= " OR LOWER(post_title) LIKE '%$nume_searchq%'";

    if ( isset( $spec_searchq ) && $spec_searchq !== '' )
        $querystr .= " OR LOWER(post_content) LIKE '%$spec_searchq%'";

    if ( isset( $instit_searchq ) && $instit_searchq !== '' )
        $querystr .= " OR LOWER(post_content) LIKE '%$instit_searchq%'";

    $querystr .= " LIMIT $limit ";

    break;

When you do this:

$querystr = "SELECT * FROM `wp_posts` WHERE post_content LIKE '%Alba%' AND post_type='post' OR LOWER(post_title) LIKE '%$nume_searchq%' OR LOWER(post_content) LIKE '%$spec_searchq%' OR LOWER(post_content) LIKE '%$instit_searchq%' LIMIT $limit ";

If ANY of the variables are blank ($nume_searchq || $spec_searchq || $instit_searchq ), you will get all posts, because "LIKE '%%'" matches everything.

So this leaves you with a couple of options. 1) Explicitly set each of those variables every time --or-- 2) Conditionally include them depending on if they contain something valuable.

For example:

case "AB":
    $querystr = "SELECT * FROM `wp_posts` WHERE post_content LIKE '%Alba%' AND post_type='post'";

    if ( isset( $nume_searchq ) && $nume_searchq !== '' )
        $querystr .= " OR LOWER(post_title) LIKE '%$nume_searchq%'";

    if ( isset( $spec_searchq ) && $spec_searchq !== '' )
        $querystr .= " OR LOWER(post_content) LIKE '%$spec_searchq%'";

    if ( isset( $instit_searchq ) && $instit_searchq !== '' )
        $querystr .= " OR LOWER(post_content) LIKE '%$instit_searchq%'";

    $querystr .= " LIMIT $limit ";

    break;

When you do this:

$querystr = "SELECT * FROM `wp_posts`  
WHERE post_content 
    LIKE '%Alba%' 
    AND post_type='post'  
OR 
    LOWER(post_title) 
    LIKE '%$nume_searchq%'  
OR 
    LOWER(post_content) 
    LIKE '%$spec_searchq%'  
OR 
    LOWER(post_content) 
    LIKE '%$instit_searchq%'  
LIMIT $limit ";

If ANY of the variables are blank ($nume_searchq || $spec_searchq || $instit_searchq ), you will get all posts, because LIKE '%%' matches everything.

So this leaves you with a couple of options.

  1. Explicitly set each of those variables every time --or--

  2. Conditionally include them depending on if they contain something valuable.

For example:

case "AB":
    $querystr = "SELECT * FROM `wp_posts` WHERE post_content LIKE '%Alba%' AND post_type='post'";

    if ( isset( $nume_searchq ) && $nume_searchq !== '' )
        $querystr .= " OR LOWER(post_title) LIKE '%$nume_searchq%'";

    if ( isset( $spec_searchq ) && $spec_searchq !== '' )
        $querystr .= " OR LOWER(post_content) LIKE '%$spec_searchq%'";

    if ( isset( $instit_searchq ) && $instit_searchq !== '' )
        $querystr .= " OR LOWER(post_content) LIKE '%$instit_searchq%'";

    $querystr .= " LIMIT $limit ";

    break;
Source Link
BA_Webimax
  • 321
  • 1
  • 8

When you do this:

$querystr = "SELECT * FROM `wp_posts` WHERE post_content LIKE '%Alba%' AND post_type='post' OR LOWER(post_title) LIKE '%$nume_searchq%' OR LOWER(post_content) LIKE '%$spec_searchq%' OR LOWER(post_content) LIKE '%$instit_searchq%' LIMIT $limit ";

If ANY of the variables are blank ($nume_searchq || $spec_searchq || $instit_searchq ), you will get all posts, because "LIKE '%%'" matches everything.

So this leaves you with a couple of options. 1) Explicitly set each of those variables every time --or-- 2) Conditionally include them depending on if they contain something valuable.

For example:

case "AB":
    $querystr = "SELECT * FROM `wp_posts` WHERE post_content LIKE '%Alba%' AND post_type='post'";

    if ( isset( $nume_searchq ) && $nume_searchq !== '' )
        $querystr .= " OR LOWER(post_title) LIKE '%$nume_searchq%'";

    if ( isset( $spec_searchq ) && $spec_searchq !== '' )
        $querystr .= " OR LOWER(post_content) LIKE '%$spec_searchq%'";

    if ( isset( $instit_searchq ) && $instit_searchq !== '' )
        $querystr .= " OR LOWER(post_content) LIKE '%$instit_searchq%'";

    $querystr .= " LIMIT $limit ";

    break;