[wp-hackers] posts_where filter - only apply at certain times (getting 404 error on single page)

Robert Lusby nanogwp at gmail.com
Sun Jan 15 22:20:34 UTC 2012

Trying to use the posts_where filter to add an extra "meta_key" check. 
(Basically posts must have (Key A value = 1 or Key A value = 2) AND Key 
B must not be empty).

Got it all setup working fine on the front page / listing page / search 
etc - however when viewing a "singluar" page of the custom post type, 
I'm getting a 404 error.

if (!is_singular('videos')) {} is being ignored at the time I apply the 
filter (returns false), so it isn't as simple as adding that around the 

Can anyone see where I'm going wrong? Or know a better way of doing it? 
Or know how I can make this filter be ignored on a singular page?

Code below, or at: http://pastebin.com/5gaAQZM4


if (!is_singular('videos')) {
function posts_where($where,$query) {

     global $wpdb;

     if (!is_admin()) {

         if ($query->query_vars['post_type'] == 'videos') {

             $new_where = "({$wpdb->postmeta}.meta_key = 'videoid' AND 
{$wpdb->postmeta}.meta_value != '')";

             if (empty($where))
                 $where = $new_where;
                 $where = "{$where} AND {$new_where}";



     return $where;


More information about the wp-hackers mailing list