get_post query where posts don’t have a specific meta_key
-
Hello
With get_post I can get all the posts with a specific meta value.
like in:$myposts = get_posts('category=3&meta_key=feature-sidebar&meta_value=true');
But I want to get all the post wich don’t have the meta_key “sidebar” but are also in category 3. So exclude al posts in category 3 that have a meta_key “sidebar”.
is this possible without sql
Thanks
-
try:
$myposts = get_posts('category=3&meta_key=feature-sidebar&meta_value!=true');
Might not work but != means does not equal. So it might work *shrugs*.
@ jimmyt1988
Thanks. I’ve tried it and it doesn’t appear to be working. It still shows the posts with the meta_key “feature-sidebar”.Can you refer to this:
http://wordpress.org/support/topic/319418?replies=15
I had a good read through it, It’s a little over my head. I hope that the above post triggers others to respond or yourself to understand.
Sorry jimmyt1988 but that topic has nothing to do with mine so I won’t refer.
Anyhow I found out how to do it with sql:$querystr = " SELECT ID, post_title, post_content FROM $wpdb->posts LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE not exists ( SELECT * FROM $wpdb->postmeta WHERE ($wpdb->postmeta.post_id = $wpdb->posts.ID) AND meta_key = 'feature-sidebar') AND (post_status = 'publish' OR post_status = 'static') AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id = 3 "; $myposts= $wpdb->get_results($querystr);
Should I make this post resolved now? I’m hoping on a cleaner solution (or a better sql query).
Ah, sorry If I wasted your time. I’m glad you found it !!! In fact, you helped me in the end. Cheers!
That solution is good. I reckon there is a quicker one. Wait till one of the gurus come on. They might give u a 2 liner hehe. Good luck in future.
Sorry to hijack this thread, but I have a similar problem. So I’m wondering if anyone has a clean solution to this problem?
OK, I figured out how to solve this a bit more elegantly. Add these codes to wp-content/themes/[theme-directory]/functions.php
add_filter('query_vars', 'metakey_queryvars' ); function metakey_queryvars( $qvars ) { $qvars[] = 'not_meta_key'; return $qvars; } add_filter('posts_where', 'metakey_where' ); function metakey_where( $where ) { global $wp_query; global $wpdb; if( isset( $wp_query->query_vars['not_meta_key'] )) { $where .= $wpdb->prepare(" AND $wpdb->posts.ID NOT IN ( SELECT post_id FROM $wpdb->postmeta WHERE ($wpdb->postmeta.post_id = $wpdb->posts.ID) AND meta_key = %s) ", $wp_query->query_vars['not_meta_key']); } return $where; }
What the above codes do is adding a custom parameter called ‘not_meta_key’. So now you can do this:
$myposts = get_posts('category=3¬_meta_key=feature-sidebar');
- The topic ‘get_post query where posts don’t have a specific meta_key’ is closed to new replies.