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

Mr.Brown mrbrog at gmail.com
Wed Jan 4 20:13:54 GMT 2006

Hi all,

I'm sorry if this is not the right place to post a support request, I
posted this also in the Forum, but I think that is a question quite

Well, this is my problem: I add a Custom Field to some posts to show
them in a news ticker in my blog header. The Key is 'ticker' and its
value is '1'.
Now, I want to exclude these posts from the Loop in the home, and show
only posts that don't have that custom field (ticker).
I tried with a custom query like:

$arts = $wpdb->get_results("SELECT P2C.*, POST.ID, POST.post_title,
FROM $wpdb->posts POST, $wpdb->postmeta P2C
WHERE POST.ID = P2C.post_id AND P2C.meta_key != 'ticker' AND
POST.post_status = 'publish'
ORDER BY POST.post_date DESC");

but the problem is that if a Post has another custom field, the query
doesn't work, probably because there's more than a record in the
wp_postmeta table with the same post_id.

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 read also the Query Posts Page on the Wiki, but it seems applicable
only to categories.

Any ideas?
Thanks in advance.


More information about the wp-hackers mailing list