Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude Multiple ID's from MySQL query

Tags:

mysql

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.

like image 481
user892670 Avatar asked Feb 08 '26 05:02

user892670


1 Answers

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.

like image 68
Sarfraz Avatar answered Feb 12 '26 02:02

Sarfraz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!