Manish Suthar
Manish Suthar

Reputation: 48

Mysql query not running with or condition

$query  = $_GET['search'];
$wpdb->get_results("SELECT * FROM wp_posts WHERE (post_type = 'discussion' AND post_status = 'publish' AND (post_content like '%".$query."%' OR post_title like '%".$query."%))");

In this query OR Condition is not running.
Any help would be appreciated!

Upvotes: 0

Views: 89

Answers (2)

Štefan Ondáš
Štefan Ondáš

Reputation: 368

SQL command looks correct.
If $query is really not empty you can try this (sometimes helps if you define returned type):

$sql = 'your select';
$results = $wpdb->get_results($sql, OBJECT);
foreach ($results as $res) { ... }

Upvotes: 0

Arulkumar
Arulkumar

Reputation: 13237

You missed a single quote in this place "%))");. So the working code will be:

$wpdb->get_results("SELECT * FROM wp_posts WHERE (post_type = 'discussion' AND post_status = 'publish' AND (post_content like '%".$query."%' OR post_title like '%".$query."%'))");

The post_title like having a starting ' but doesn't have the closing '

Upvotes: 1

Related Questions