Link to search page without search parameter

Create a file search.php inside your theme-folder. In this file you can design the search page and its functionality. Typically search page looks like this, <?php /** * The template for displaying search results pages * * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result * * @package Torba_Market */ get_header(); ?> <div class=”container”> <section id=”primary” class=”content-area”> <main id=”main” class=”site-main” role=”main”> … Read more

Extending search query with additional $sentence value

Gladly I’ve written two plugins for that yesterday: Filter/Core That’s what a search query part looks like inside the posts_search filter: ‘ AND (((wp_XX_posts.post_title LIKE ‘%test%’) OR (wp_XX_posts.post_content LIKE ‘%test%’))) ‘ where wp_XX_ is just the $wpdb->prefix for my WPSE test site inside my local MU installation. Plugin #1 – drop searching post types that … Read more

Search by Post ID and display content of the post in search result

Put any of the filters in the functions.php Using pre get posts // Filter the search page add_filter(‘pre_get_posts’, ‘search_pre_get_posts’); function search_pre_get_posts($query) { // Verify that we are on the search page that that this came from the event search form if($query->query_vars[‘s’] != ” && is_search()) { // If “s” is a positive integer, assume post … Read more

How do I redirect /search/ to wordpress search template?

You can use template_redirect. Here a simple redirection function. add_action( ‘template_redirect’, ‘se219663_template_redirect’ ); function se219663_template_redirect() { global $wp_rewrite; if ( is_search() && ! empty ( $_GET[‘s’] ) ) { $s = sanitize_text_field( $_GET[‘s’] ); // or get_query_var( ‘s’ ) $location = “https://wordpress.stackexchange.com/”; $location .= trailingslashit( $wp_rewrite->search_base ); $location .= user_trailingslashit( urlencode( $s ) ); $location … Read more

How to insert a span inside a search form?

If we look at the source code of get_search_form(), notice that before the form gets rendered, the search_form_format filter hook gets fired. We can use that to add another filter attached to get_search_form where the callback is dependent upon the format. add_filter( ‘search_form_format’, ‘wpse_259716_search_form_format’, 99, 1 ); function wpse_259716_search_form_format( $format ) { if( in_array( $format, … Read more

How do I filter the search results order?

You can use the posts_orderby filter to alter the order of returned posts. This will run for every query (front and back), so make sure you ensure you want to alter the order by using is_admin, is_search etc. In the example below search results are ordered by post type in ascending order (e.g. page then … Read more