• Resolved lucius100

    (@lucius100)


    Hi, I wonder if my search results is normal

    I generated data with fakerpress, and I pasted in the keyword “Exercitationem autem occaecati nobis” , it display any custom post type with any keywords in it, if the post contain any of the word autem,nobis,Exercitationem,occaecati in the title or the body content. It will display the post results. Is it normal ?

    How do I set it so the words need to match all the string, or it need to match the title only not in the body content ?

    I use the default function provided in doc

    function my_cptui_add_post_type_to_search( $query ) {
    	if ( is_admin() || ! $query->is_main_query() ) {
    		return;
    	}
    
    	if ( $query->is_search() ) {
    		$query->set(
    			'post_type',
    			array( 'article' ),
    		);
    	}
    }
    add_filter( 'pre_get_posts', 'my_cptui_add_post_type_to_search' );
    • This topic was modified 3 years, 8 months ago by lucius100.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Yes, I do believe that that’s default nature for WordPress, it’ll take a search query and separate out all the parts in the passed search phrase, looking for instances of any of them.

    Your usage of that snippet above is more for including “article” posts in the search results, but not for controlling how the search phrase is treated.

    That said though, I did a search, pun intended, and found that there is a “sentence” parameter available that will make it so the phrase has to match exactly. For example I have a post with “thing” in it, and then another with “the thing” and when I search “the thing” with no settings changed, it turned up both posts. However, when i set the “sentence” parameter, it returned just the post with “the thing” in it.

    So, borrowing from our code above, it should instead be

    function my_cptui_add_post_type_to_search( $query ) {
    	if ( is_admin() || ! $query->is_main_query() ) {
    		return;
    	}
    
    	if ( $query->is_search() ) {
    		$query->set(
    			'sentence',
    			true,
    		);
    	}
    }
    add_filter( 'pre_get_posts', 'my_cptui_add_post_type_to_search' );
    

    Do note that this will affect ALL queries, but I am suspecting that’s desired for your case.

    Thread Starter lucius100

    (@lucius100)

    Well, if it’s behavior is normal like that, it’s fine.

    Just want to know if there’s other available options to get other search results as specified above.

    How about the search query to match title only and exclude body content ?
    Is it possible ?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    The full sentence version is definitely available, and I’d need to do some googling to see if or how to limit it to just certain parts of a given post, like just title, or just post content, etc. I don’t know for certain at the moment.

    Thread Starter lucius100

    (@lucius100)

    Ok sir, thank you.
    It has been a great help ??

    Thread Starter lucius100

    (@lucius100)

    I found my solution already, thanks, gonna close the sup thread.

    • This reply was modified 3 years, 8 months ago by lucius100.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘CPT UI search result’ is closed to new replies.