• hoven002

    (@hoven002)


    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: https://www.kennethweb.nl

    Thanks,
    Kenneth

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    try changing this [untested]:

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

    to this:

    if ($query->is_search && !$query->is_admin) {

    Thread Starter hoven002

    (@hoven002)

    Thanks for the suggestion, but it doesn’t do the trick…

    Moderator keesiemeijer

    (@keesiemeijer)

    Why not delete the term test?

    Thread Starter hoven002

    (@hoven002)

    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…

    Moderator keesiemeijer

    (@keesiemeijer)

    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?

    Thread Starter hoven002

    (@hoven002)

    Yes I’m sure…

    It’s like this:

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

    Do you have other nice ideas? Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Exclude term from search query’ is closed to new replies.