3

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.

1
  • 1
    Can't you use WHERE ID IN('1', '5', '235'); Commented Jun 14, 2012 at 17:24

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.