WordPress.org

Ready to get started?Download WordPress

Forums

Custom Search without "s" variable (2 posts)

  1. kinkersnick
    Member
    Posted 1 year ago #

    I've created a custom search for my blog, which searches custom post types by all sorts of post_meta values.
    I've got the actual search working great, but I was building it on a homepage to test it, so the form submits to the same page and the loop of results is also displayed there. Everything working.

    But now to implement it, I want the results to appear on a new page (I suppose a custom version of 'search.php') but I'm confused and can't get it to work.

    I was using a search form like this (this is without all the extra <input> sections):

    <form name="search" id="searchform" action="<?php bloginfo('url'); ?>/" method="get" role="search">
    <input type="text" class="field" name="keyword" id="keyword" placeholder="<?php esc_attr_e( 'Enter to search &rarr;', 'agility' ); ?>" value="<?php if(isset( $_GET['keyword'] ) ){ echo $keyword; }?>"/>
    <input type="submit" class="submit" name="submit" id="searchsubmit" value="Search" />

    However, this fails to send everything to search.php. I figured out that for some reason WordPress needs to have the "s" variable or it doesn't work. If I was to change the second line to:

    <input type="text" class="field" name="s" id="s" placeholder="<?php esc_attr_e( 'Enter to search &rarr;', 'agility' ); ?>" value="<?php if(isset( $_GET['keyword'] ) ){ echo $keyword; }?>"/>

    Then it works fine. The problem is, I want people to be able to search without having to put anything in the keyword field. For example, they might choose from a drop-down to search everything located in the UK, but the search form submits with "s=" (no value for 's') and so WordPress doesn't go to search.php at all, instead just returning a standard blog loop page.

    Is there some way to get around this?

    All I want is to be able to submit my search to a page that can handle the custom search.

    Any ideas?

    Thanks, and hope this makes sense.

  2. Bonesnap
    Member
    Posted 1 year ago #

    You have to create a custom template for a page that's expecting to receive your search form. In the action="" attribute of the form, point to that page. This will allow you to create a custom search.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.