0

I'm trying to prepend /team to the search results page in wordpress. Currently my form has the correct action and goes to the correct url on search but the page is 404ing.

<form class="team-wrapper__search" method="get" action="<?php echo home_url('/team/'); ?>">
  <input type="hidden" name="search-type" value="team">
  <input type="text" name="s" value="<?php the_search_query(); ?>" placeholder="Search for a specialist" required="required">
  <button type="submit">Search</button>
</form>

If I go to domainname.com/?search-type=team&s=adam the search results are displayed correctly. But I need to make it so that the results display in domainname.com/team/?search-type=team&s=adam. As of now this page hits a 404.

The client has also installed a plugin called Ajax search pro but unsure how relevant this is to the question.

1 Answer 1

0

Add a rewrite rule to handle the custom search URL:

In your theme's functions.php file, add the following code:

function custom_search_rewrite_rule() {
    add_rewrite_rule(
        'team/?$',
        'index.php?s=',
        'top'
    );
    add_rewrite_rule(
        'team/page/([0-9]{1,})/?$',
        'index.php?s=&paged=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_search_rewrite_rule');

Flush rewrite rules:

After adding the above code, you need to flush the rewrite rules. The easiest way to do this is by visiting the Permalinks settings page in the WordPress admin (Settings -> Permalinks) and clicking the Save Changes button.

Modify the search form action URL:

Update your search form to include the search-type parameter directly in the action URL. Change the form action to /team/ and keep the query parameters intact:

<form class="team-wrapper__search" method="get" action="<?php echo home_url('/team/'); ?>">
    <input type="hidden" name="search-type" value="team">
    <input type="text" name="s" value="<?php the_search_query(); ?>" placeholder="Search for a specialist" required="required">
    <button type="submit">Search</button>
</form>

Handle the search results:

To ensure WordPress understands that the custom URL should display search results, modify the search query if the search-type parameter is present. Add the following code to your functions.php file:

function custom_search_query($query) {
    if (!is_admin() && $query->is_main_query() && $query->is_search()) {
        if (isset($_GET['search-type']) && $_GET['search-type'] == 'team') {
            $query->set('post_type', 'team'); // Replace 'team' with your custom post type if needed
        }
    }
}
add_action('pre_get_posts', 'custom_search_query');
1
  • Appreciate your response. It has worked now and team/?search-type=team&s=john is displaying the results. Only problem is now the /team/ archive page is a blank white page. I've tried looking into the debug log but there's nothing relevant. I think it might be something to do with the rewrite rule.
    – Provolkata
    Commented Jul 8 at 16:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.