Displaying number of search results for each post type

First, your callback to pre_get_posts is wrong. That is not the way that set works. If you had debugging enabled, you’d see Notices. It should be: function more_posts_per_search_page( $query ) { if ( !is_admin() && $query->is_main_query() ) { if ( $query->is_search ) { $query->set(‘posts_per_page’,500); $query->set(‘post_type’,array( ‘author’, ‘book’)); } } } add_action( ‘pre_get_posts’,’more_posts_per_search_page’ ); Second, there … Read more

Wildcard search in WP Query

WP_Query will add the % wildcard operators for a LIKE query. Just remove those. As is your query looks like LIKE ‘%\\%rocket\\%% which is not what you want. Try var_dump($query->request); And you can see that for yourself.

How to add one more query string with search query string

You should use ‘add_query_arg’ function, to do this. It adds a single key & value or an associative array. You can set a key value to false to remove it from the query. Omitting the old query or the uri (second or third parameter) uses the $_SERVER value. So in your case doing it like … Read more

How do i append UNION SQL Query with default search query

Append data to the native search: You can try for example: add_action( ‘pre_get_posts’, function( $q ) { if( $q->is_main_query() && $q->is_search() ) { $q->set( ‘nopaging’, true ); add_filter( ‘posts_request’, function( $request ) { $sql=”YOUR EXTRA SQL QUERY”; return “({$request}) UNION ({$sql}) ORDER BY post_status DESC”; }); } }); where we use the no paging version … Read more

Search.php display results differently based on post type

You can do something like this inside your loop if ( ‘machine’ == get_post_type() ){ //code for machine post types }elseif ( ‘post’ == get_post_type()){ //code for posts }elseif ( ‘page’ == get_post_type()){ // code for pages } else{ //code for anything else }

Make search result display 1 Company or 2 Companies

$found_posts holds the amount of posts found by a certain query. You can use this logic to display your links Example: if ( $allsearch->found_posts <= 1 ) { //Dispaly company } else { // Disply companies } Just a note on grammar, it should be 0 companies 1 company and x amount of companies after … Read more

Limit search to a specific folder in website

There is not “blog” folder in WordPress and the search is not done in folders, it is done in the database. I assume you mean limit the search to standard post types. You could add post_type argument to the search form to include only the standard post type: <form method=”get” class=”searchform” action=”<?php echo esc_url(home_url()); ?>/”> … Read more

How to reset usual $query on search page to push custom $wpdb query there?

This is not how pre_get_posts works. The pre_get_posts action gives developers access to the $query object by reference (any changes you make to $query are made directly to the original object – no return value is necessary). https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts What you are doing is simply wrong. You don’t “return” another query via pre_get_posts, you alter the … Read more