<?php
/*
Plugin Name: WP Custom Fields Search - Post Type Hack
Plugin URI: http://www.webhammer.co.uk/
Description: Hacky fix for a problem which occurs on certain sites.  I can't reproduce the problem, possibly it's an incompatibility with certain themes/plugins.
Version: 0.0.1-alpha
Author: Don Benjamin
Author URI: http://www.webhammer.co.uk/
Text Domain: wp_custom_fields_search_post_type-hack
*/

class WPCustomFieldsSearchShowAllPostTypes
{
    private function __construct()
    {
        add_action('posts_where', array($this,'posts_where'), 9);
    }

    public static function getInstance()
    {
        static $instance;
        if (!$instance) {
            $instance = new WPCustomFieldsSearchShowAllPostTypes();
        }
        return $instance;
    }

    public function posts_where($where)
    {
        if (!class_exists('WPCustomFieldsSearchPlugin')) {
            return $where;
        }

        global $wpdb;
        $wpSearch = WPCustomFieldsSearchPlugin::getInstance();

        if ($wpSearch->is_search_submitted()) {
            $where = preg_replace(
                "/AND\s*$wpdb->posts.post_type\s*=\s*'(post|page)'/",
                "",
                $where
            );
            $where = preg_replace(
                "/AND\s*$wpdb->posts.post_type\s*IN\s*\([^\)]*\)/",
                "",
                $where
            );
        }
        return $where;
    }
}
WPCustomFieldsSearchShowAllPostTypes::getInstance();
