Пример #1
0
function custom_taxonomy_search_where($where)
{
    global $wpdb, $wp_query, $cp_options;
    $old_where = $where;
    // intercept the old where statement
    $query = '';
    // get the custom fields to add to search
    $customfields = cp_custom_search_fields();
    if (is_search()) {
        $customsearch_params = $_GET;
        foreach ($customsearch_params as $key => $val) {
            if (strpos(' ' . $key, 'cp_') == 1 && trim($val) != '') {
                $customs[$key] = $val;
            }
        }
        if (isset($customs)) {
            foreach ($customs as $key => $val) {
                $key = str_replace(" ", "_", $key);
                $query .= " AND mjoin_" . $key . ".meta_value = '" . $val . "'";
            }
        }
    }
    //remove_filter( 'posts_where', 'custom_taxonomy_search_where' );
    //$where .= $query;	// rj uncomment this line to add custom taxonomy in search
    return $where;
}
Пример #2
0
/**
 * Builds the WHERE part in search queries.
 *
 * @param string $where
 *
 * @return string
 */
function custom_search_where($where)
{
    global $wpdb, $wp_query, $cp_options;
    $old_where = $where;
    // intercept the old where statement
    if (is_search() && isset($_GET['s'])) {
        if (!cp_search_index_enabled()) {
            // get the custom fields to add to search
            $customs = cp_custom_search_fields();
            // add some internal custom fields to search
            $customs = array_merge($customs, array('cp_sys_ad_conf_id'));
        }
        $query = '';
        $var_q = stripslashes($_GET['s']);
        //empty the s parameter if set to default search text
        if (__('What are you looking for?', APP_TD) == $var_q) {
            $var_q = '';
        }
        if (isset($_GET['sentence']) || $var_q == '') {
            $search_terms = array($var_q);
        } else {
            preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $var_q, $matches);
            $search_terms = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
        }
        if (!isset($_GET['exact'])) {
            $_GET['exact'] = '';
        }
        $n = $_GET['exact'] ? '' : '%';
        $searchand = '';
        foreach ((array) $search_terms as $term) {
            $term = addslashes_gpc($term);
            $query .= "{$searchand}(";
            if (!cp_search_index_enabled()) {
                $query .= "({$wpdb->posts}.post_title LIKE '{$n}{$term}{$n}')";
                $query .= " OR ({$wpdb->posts}.post_content LIKE '{$n}{$term}{$n}')";
                $query .= " OR ((t.name LIKE '{$n}{$term}{$n}')) OR ((t.slug LIKE '{$n}{$term}{$n}'))";
                foreach ($customs as $custom) {
                    $query .= " OR (";
                    $query .= "(m.meta_key = '{$custom}')";
                    $query .= " AND (m.meta_value LIKE '{$n}{$term}{$n}')";
                    $query .= ")";
                }
            } else {
                $query .= "({$wpdb->posts}.post_content_filtered LIKE '{$n}{$term}{$n}')";
            }
            $query .= ")";
            $searchand = ' AND ';
        }
        $term = esc_sql($var_q);
        if (!isset($_GET['sentence']) && count($search_terms) > 1 && $search_terms[0] != $var_q) {
            if (!cp_search_index_enabled()) {
                $query .= " OR ({$wpdb->posts}.post_title LIKE '{$n}{$term}{$n}')";
                $query .= " OR ({$wpdb->posts}.post_content LIKE '{$n}{$term}{$n}')";
            } else {
                $query .= " OR ({$wpdb->posts}.post_content_filtered LIKE '{$n}{$term}{$n}')";
            }
        }
        if (!empty($query)) {
            $where = " AND ({$query}) AND ({$wpdb->posts}.post_status = 'publish') ";
            // setup the array for post types
            $post_type_array = array();
            // always include the ads post type
            $post_type_array[] = APP_POST_TYPE;
            // check to see if we include blog posts
            if (!$cp_options->search_ex_blog) {
                $post_type_array[] = 'post';
            }
            // check to see if we include pages
            if (!$cp_options->search_ex_pages) {
                $post_type_array[] = 'page';
            }
            // build the post type filter sql from the array values
            $post_type_filter = "'" . implode("','", $post_type_array) . "'";
            // return the post type sql to complete the where clause
            $where .= " AND ({$wpdb->posts}.post_type IN ({$post_type_filter})) ";
        }
        remove_filter('posts_where', 'custom_search_where');
    }
    return $where;
}
Пример #3
0
/**
 * Register items to index, post types, taxonomies, and custom fields
 *
 * @return void
 */
function cp_register_search_index_items()
{
    if (!current_theme_supports('app-search-index') || isset($_GET['firstrun'])) {
        return;
    }
    // Ad listings
    $listing_custom_fields = array_merge(cp_custom_search_fields(), array('cp_sys_ad_conf_id'));
    $listing_index_args = array('meta_keys' => $listing_custom_fields, 'taxonomies' => array(APP_TAX_CAT, APP_TAX_TAG));
    APP_Search_Index::register(APP_POST_TYPE, $listing_index_args);
    // Blog posts
    $post_index_args = array('taxonomies' => array('category', 'post_tag'));
    APP_Search_Index::register('post', $post_index_args);
    // Pages
    APP_Search_Index::register('page');
}