WordPress.org

Ready to get started?Download WordPress

Forums

Creating a custom search result page (4 posts)

  1. Ude
    Member
    Posted 2 years ago #

    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?

    -Ude

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    This will not be easy.
    Try making your own search form: http://codex.wordpress.org/Function_Reference/get_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:
    http://codex.wordpress.org/Function_Reference/query_posts
    http://codex.wordpress.org/Function_Reference/WP_Query

    http://codex.wordpress.org/Creating_a_Search_Page#Preserving_Search_Page_Results_and_Pagination

  3. Ude
    Member
    Posted 2 years ago #

    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?

  4. keesiemeijer
    moderator
    Posted 2 years ago #

    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:

    <?php
    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 -->

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags