Support » Themes and Templates » Creating a custom search result page

  • Hi guys,

    I’ve made a separate gallery page that only displays attachments from all the posts in the site. Now I’m trying to add a search field there that would filter down the attachments that match the given keyword.

    The problem: how should I divert WP from using the default search.php (which is used now for normal post search) and use a custom gallery search result page instead (search_gallery.php)?

    In what function should the magic happen?


Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer



    This will not be easy.
    Try making your own search form:
    Give this searchform a hidden input when on a gallerypage:

    <input type="hidden" name="gallerysearch" id="gallerysearch" value="on" />

    Check for this value (before the loop) when on a search result page (search.php):

    if(isset($_REQUEST['gallerysearch']) && $_REQUEST['gallerysearch'] == 'on') {
    // use a query post here.

    If the value “gallerysearch” exists query the posts showing only attachments:

    Superthanks for giving these instructions, they helped me a lot!

    Things are working almost as planned now, I have a search field that matches keywords with the posts’ data and it displays all the images related to those, but I can’t get it to search the attachments’ own titles or descriptions.

    Using the Search Everything-plugin didn’t help. Is this a limitation of WP search or is there a way to include attachment titles and descriptions to the search in the code?

    Moderator keesiemeijer



    but I can’t get it to search the attachments’ own titles or descriptions.

    Explain more what you mean by this:

    if I use this before the loop (in search.php) it searches in image titles and descriptions:

    global $query_string;
    $query_args = explode("&", $query_string);
    $search_query = array();
    foreach($query_args as $key => $string) {
    	$query_split = explode("=", $string);
    	$search_query[$query_split[0]] = urldecode($query_split[1]);
    } // foreach
    $args = array('post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit');
    $search_query = array_merge($search_query,$args);
    $search = new WP_Query($search_query);
    <?php if ( $search->have_posts() ) : while ( $search->have_posts() ) : $search->the_post(); ?>
    <!-- rest of loop -->

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Creating a custom search result page’ is closed to new replies.