[wp-hackers] Excluding from the Loop posts having a certain Custom Field

John Joseph Bachir jjb at ibiblio.org
Wed Jan 4 23:15:10 GMT 2006

On Wed, 4 Jan 2006, Mr.Brown wrote:

> I'm sorry if this is not the right place to post a support request

You're right, it's not : )

> I posted this also in the Forum, but I think that is a question quite 
> complex...

It might be complex in certain ways, but the question isn't 
wordpress-specific. This question is more appropriate for an sql list or 
irc channel.

But we are a friendly bunch, so here you go.. :)

> I could use this
> $arts = $wpdb->get_results("SELECT ID,post_title,post_date FROM
> $wpdb->posts WHERE post_status = 'publish' AND ID NOT IN (SELECT
> post_id FROM $wpdb->postmeta WHERE meta_key = 'ticker') ORDER BY
> post_date DESC");
> but unfortunately doesn't work with mySQL version available on my
> hosting service.

I'm assuming by that you mean that your hosting service is running mySQL < 
4.1 and therefore can't handle subqueries.

you can just do two queries:

$tickerposts = $wpdb->get_column("
         SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'ticker'

$tickerposts = implode(',',$tickerposts);

$arts = $wpdb->get_results("
         SELECT ID,post_title,post_date
         FROM $wpdb->posts
 		post_status = 'publish' AND
         	ID NOT IN ($tickerposts)
         ORDER BY post_date DESC


More information about the wp-hackers mailing list