user892670
user892670

Reputation: 197

Exclude Multiple ID's from MySQL query

Hoping this is just a case of syntax.

I'm writing a custom search function for Wordpress and it's all working great except I'd like to exclude a couple of results dependant on their ID's.

This works ok with one ID

$sqlp_page ="select ID, post_title, post_name, post_excerpt from wp_posts where post_type='page' and ID != '236' ";
$sqlp_page .="and post_status='publish' ";
$sqlp_page .="and (post_title like '%".$_GET['s']."%' ";
$sqlp_page .="or post_content like '%".$_GET['s']."%') ";
$sqlp_page .="and post_status='publish' ";
$sqlp_page .="order by id ASC ";

But I can't seem to pass in more than one value for the ID. I have searched the net and tried several different ways but nothing seems to work for me.

$sqlp_page ="select ID, post_title, post_name, post_excerpt from wp_posts where post_type='page' and ID != '236,239' ";

Or

$sqlp_page ="select ID, post_title, post_name, post_excerpt from wp_posts where post_type='page' and ID != '236' or '239' ";

And

$sqlp_page .="and ID != '236' ";
$sqlp_page .="and ID != '239' ";

But nothing seems to work. Any assistance is greatly appreciated.

Upvotes: 3

Views: 9124

Answers (1)

Sarfraz
Sarfraz

Reputation: 382686

Use NOT IN:

$sqlp_page ="select ID, post_title, post_name, post_excerpt 
from wp_posts where post_type='page' and ID NOT IN ('236','239') ";

Inside NOT IN, you need to separate multiple ID values with a comma.

Upvotes: 12

Related Questions