Forums

Exclude term from search query (7 posts)

  1. hoven002
    Member
    Posted 6 months ago #

    Hello,

    I have a website with listings ( custom post type) with some taxonomies, like:
    - woningtype
    - interieur, etc..
    On some pages I have a search function which uses the taxonomy 'Woningtype' with
    the terms:
    - Shortstay
    - Verhuur
    - Test

    I want to exclude now the term 'Test' from the search function; i.d. don't show the term 'Test'
    with the search parameter 'Woningtype'.

    I am now using the following code in the file functions.php:

    function be_modify_search_query( $query ) {
    	global $wp_the_query;
    	if( $query === $wp_the_query && $query->is_search() ) {
    		$tax_query = array(
    			array(
    				'taxonomy' => 'woningtype',
    				'field' => 'slug',
    				'terms' => 'test',
    				'operator' => 'NOT IN',
    			)
    		);
    		$query->set( 'tax_query', $tax_query );
    	}
    }
    add_action( 'pre_get_posts', 'be_modify_search_query' );

    [Code moderated: please ensure that your code is enclosed in backticks (`) or use the code button.]

    It still doesn't work! Somebody has a idea?
    The website is: http://www.kennethweb.nl

    Thanks,
    Kenneth

  2. keesiemeijer
    moderator
    Posted 6 months ago #

    try changing this [untested]:

    if( $query === $wp_the_query && $query->is_search() ) {

    to this:

    if ($query->is_search && !$query->is_admin) {
  3. hoven002
    Member
    Posted 6 months ago #

    Thanks for the suggestion, but it doesn't do the trick...

  4. keesiemeijer
    moderator
    Posted 6 months ago #

    Why not delete the term test?

  5. hoven002
    Member
    Posted 6 months ago #

    Good question. I'm gonna use the term 'Test' for special listings and wanna publish them on a seperate page, so I need to use the taxonomies to complete the listings...without showing these listings in the search function...

  6. keesiemeijer
    moderator
    Posted 6 months ago #

    Are you sure the taxonomy you registered is "woningtype"? If I use "'taxonomy' => 'post_tag'" (normal tags) your code with my alteration works?

    Or is "woningtype" a custom post type?

  7. hoven002
    Member
    Posted 6 months ago #

    Yes I'm sure...

    It's like this:

    taxonomies - woningtype
    term - shortstay, verhuur, test
    etc..

    Do you have other nice ideas? Thanks!

Reply

You must log in to post.

About this Topic