0

This all started because, on the front-end, I wanted the search query to include terms from taxonomies and custom meta. So, after a google, I found the following code which I added to my functions file:

    add_filter('posts_where', 'advanced_search_query' ); 
    function advanced_search_query( $where )
    {
    if( is_search() ) {

global $wpdb;
$query = get_search_query();
$query = like_escape( $query );

// include postmeta in search
 $where .=" OR {$wpdb->posts}.ID IN (SELECT {$wpdb->postmeta}.post_id FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->postmeta}.meta_key = 'bible_reference' AND {$wpdb->postmeta}.meta_value LIKE '%$query%' AND {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)";

 // include taxonomy in search
$where .=" OR {$wpdb->posts}.ID IN (SELECT {$wpdb->posts}.ID FROM {$wpdb->posts},{$wpdb->term_relationships},{$wpdb->terms} WHERE {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id AND {$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->terms}.term_id AND {$wpdb->terms}.name LIKE '%$query%')";


if(WP_DEBUG)var_dump($where);
}
return $where;
    }

It worked a treat for my front-end search. However, I've since discovered that the admin search has now gone to pot. For example, if I search in the list of pages in the dashboard, it only displays results from my custom post types. And, if I try searching again, once the first lot of results is displayed, I receive an error: 'Invalid post type'.

I've tried removing the filter using remove_filter() but nothing seems to work.

Please can someone help?

0

You can check if is_admin or not in the first line of the function

add_filter('posts_where', 'advanced_search_query' ); 
function advanced_search_query( $where )
{
    if(is_admin()){
        return $where;
    }
    if( is_search() ) {
        global $wpdb;
        $query = get_search_query();
        $query = like_escape( $query );

        // include postmeta in search
         $where .=" OR {$wpdb->posts}.ID IN (SELECT {$wpdb->postmeta}.post_id FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->postmeta}.meta_key = 'bible_reference' AND {$wpdb->postmeta}.meta_value LIKE '%$query%' AND {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)";

         // include taxonomy in search
        $where .=" OR {$wpdb->posts}.ID IN (SELECT {$wpdb->posts}.ID FROM {$wpdb->posts},{$wpdb->term_relationships},{$wpdb->terms} WHERE {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id AND {$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->terms}.term_id AND {$wpdb->terms}.name LIKE '%$query%')";


        if(WP_DEBUG)var_dump($where);
    }
    return $where;
}
| improve this answer | |
  • Thanks for getting back to me so quickly! I've amended the code with the if(is_admin()) included, but I'm still having the issue with the admin search. Any ideas? – zipadee Jun 26 '15 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.