Custom Search Query running on every page

You are missing the curly brackets {} around your query statements so the query is modified regardless of the if statement. So try modifying your code to have something like if( is_search() ) { $fields=”…”; return $fields } same thing with your $join variable Remember that while using if without brackets is acceptable for single … Read more

Change searched term

The pre_get_posts filter is the right place to go. You can check if you are on a search query, and alter it the way you want. Here’s an example: function search_filter($query) { if ( !is_admin() && $query->is_main_query() ) { // Check if we are on a search page if ($query->is_search) { // Get the search … Read more

Search Posts with Custom Fields as query

There are few ways to solve this problem… 1. Don’t bother and use Relevansi (or some other similar plugin) You can often hear that using many plugins is a bad thing. It’s not entirely true. It’s bad only if these plugins have big impact on use of resources. In this case, Relevansi won’t cause such … Read more

Search function works improperly

This code should work OK, but… There are some problems with it. Let’s break it apart and add some comments. function searchcategory($query) { if ($query->is_search) { // Checking only for is_search is very risky. It will be set to true // whenever param “s” is set. So your function will modify any wp_query with s, … Read more

Use wp_get_recent_posts with search term

It should just be a matter of setting the ‘s’ argument of wp_get_recent_posts() (or just get_posts()) to the search term: $args = array( ‘numberposts’ => $numberposts, ‘offset’ => $offset, ‘category__not_in’ => array(391), ‘category’ => $category, ‘orderby’ => ‘post_date’, ‘order’ => ‘DESC’, ‘post_type’ => ‘post’, ‘post_status’ => ‘publish’, ‘s’ => $searchterm, ); $recent_posts = wp_get_recent_posts( $args, … Read more