Last updated on September 25, 2023

Customize the WordPress Search Results Page

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Customize search results for users.

The code snippet below demonstrates how to customize the WordPress search results page by modifying the default search query and displaying custom content.

/**
 * Customize the WordPress search results page.
 */
function wpsnippets_custom_search_query( $query ) {
    if ( $query->is_search() && ! is_admin() ) {
        // Modify the search query here
        $query->set( 'post_type', 'post' );
        $query->set( 'posts_per_page', 10 );
    }
}
add_action( 'pre_get_posts', 'wpsnippets_custom_search_query' );

/**
 * Display custom content on the search results page.
 */
function wpsnippets_custom_search_content() {
    if ( is_search() ) {
        // Add your custom content here
        echo '<h2>Custom Search Results</h2>';
        echo '<p>This is a custom message for the search results page.</p>';
    }
}
add_action( 'wp', 'wpsnippets_custom_search_content' );

Use case: You want to modify the default search query to only search for posts and limit the number of search results displayed. Additionally, you want to display custom content on the search results page.

This code snippet includes two functions. The first function, wpsnippets_custom_search_query(), modifies the search query using the pre_get_posts action hook. In this example, the search query is modified to only search for posts and limit the number of search results to 10.

The second function, wpsnippets_custom_search_content(), displays custom content on the search results page using the wp action hook. In this example, a custom heading and paragraph are displayed.

You can customize the search query and the content displayed on the search results page by modifying the code within these functions.

Examples

Example 1: Customize the search results page layout

This use case demonstrates how to customize the layout of the search results page in WordPress. By modifying the search.php template file, you can control the appearance and structure of the search results.

<?php
/**
 * Custom search results page layout
 */
function wpsnippets_custom_search_results_layout() {
    if ( is_search() ) {
        // Custom code to modify the search results layout
    }
}
add_action( 'template_redirect', 'wpsnippets_custom_search_results_layout' );

In this example, we use the template_redirect action hook to target the search results page. Inside the callback function, you can add your custom code to modify the layout as desired.

Example 2: Customize the search results page query

This use case demonstrates how to customize the search query for the search results page in WordPress. By modifying the pre_get_posts action hook, you can alter the query parameters to refine the search results.

<?php
/**
 * Custom search results page query
 */
function wpsnippets_custom_search_results_query( $query ) {
    if ( $query->is_search() && ! is_admin() ) {
        // Custom code to modify the search query
    }
    return $query;
}
add_filter( 'pre_get_posts', 'wpsnippets_custom_search_results_query' );

In this example, we use the pre_get_posts filter to target the search query. Inside the callback function, you can access and modify the $query object to customize the search parameters.

Example 3: Customize the search results page content

This use case demonstrates how to customize the content of the search results page in WordPress. By modifying the the_excerpt filter, you can change the display of the search results content.

<?php
/**
 * Custom search results page content
 */
function wpsnippets_custom_search_results_content( $excerpt ) {
    if ( is_search() ) {
        // Custom code to modify the search results content
    }
    return $excerpt;
}
add_filter( 'the_excerpt', 'wpsnippets_custom_search_results_content' );

In this example, we use the the_excerpt filter to target the search results content. Inside the callback function, you can modify the $excerpt variable to customize the display of the search results content.

Last updated on September 25, 2023. Originally posted on October 6, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *