user1987095
user1987095

Reputation: 459

custom search against custom post type

In my wordpress site i need two types of search one for basic wordpress search another is custom search.both r separate .first one is ok but second one is creating problem.In custom search i have to search category and key word,Here category is custom_taxonomy and post type also custom post type. taxonomy=faq-group post_type=faq-item

example: if any one search category=Australia and keyword=visa Then it will show all post that have visa keyword and Australia category from faq module. I've searched it in google.I think,i've write custom query Thanks in advance

Upvotes: 0

Views: 186

Answers (1)

Datta Parad
Datta Parad

Reputation: 719

Use following code for achieve this

function custom_search_where( $where )
{
    if(isset($_POST['your-custom-serchtext'])){
        $where = "post_content Like %$_POST['your-custom-serchtext']% ", $where );

    }
    return $where;
}

  $query = new WP_Query( array('post_type' => 'faq-item','tax_query' => array(
        array(
            'taxonomy' => 'faq-group',
            'field' => 'slug',
            'terms' => 'australia '
        )
    )) );
  add_filter('posts_where', 'custom_search_where' );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
} else {
    // no posts found
}

remove_filter( 'posts_where', 'custom_search_where' );

I have not tested this code but I hope it will useful for you.

Upvotes: 0

Related Questions