WordPress.org

Ready to get started?Download WordPress

Forums

Search in Archives Pages by subcategory, tag, custom field and year (1 post)

  1. ameraz
    Member
    Posted 1 year ago #

    I've been struggling to find the best solution for a better (custom) search. I'm using WordPress for a large Media Catalog, so my intention is to provide better search options.

    So far I've been able to put various search forms in the archive.php that perform search by: subcategory, tag, custom field(autor) and year, all within the same Archive Category.

    I'm posting this here to see if there's any body out there that would like to improve or use what I've done so far.

    What I hate about it is having different forms for each type of search, I feel it's not user friendly at all. Perhaps somebody could help me out to build a better solution.
    Eventually I'll integrate this with jQuery to see if it can get any better, but first I want to clean up my code, I'm thinking that there could be easier and better ways to do the markup of what I've done.

    Ok, so in my archive.php I have the forms like this:

    <?php
    
    // Current URL for setting up Search Form Action
    global $wp;
    $current_url = get_home_url(null, $wp->request, null);
    
    // Current Archive Category
    $cat = get_query_var('cat');
    
    ?>
    
    <!-- Subcategory Search -->
    <form role="search" method="get" id="searchform" action="<?php echo $current_url; ?>">
    	<label for="s">Search:</label>
    	<input type="text" value="Search" name="s" id="s" id="searchbox" onfocus="if (this.value == 'Search') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search';}" />
    	<input type="hidden" name="search-type" value="normal" />
    	<?php wp_dropdown_categories('show_option_all=All Subcategories&child_of='.$cat ); ?>
    	<input id="searchsubmit" value="Search" type="submit">
    </form>
    
    <!-- Tag Search -->
    <form role="search" method="get" id="searchform" action="<?php echo $current_url; ?>">
    	<label for="s">Search Tag:</label>
    	<input type="text" value="Search" name="s" id="s" id="searchbox" onfocus="if (this.value == 'Search') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search';}" />
    	<input type="hidden" name="search-type" value="tag" />
    	<input id="searchsubmit" value="Search" type="submit">
    </form>
    
    <!-- Custom Field "autor" Search -->
    <form role="search" method="get" id="searchform" action="<?php echo $current_url; ?>">
    	<label for="s">Search in Custom Field 'autor':</label>
    	<input type="text" value="Search" name="s" id="s" id="searchbox" onfocus="if (this.value == 'Search') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search';}" />
    	<input type="hidden" name="search-type" value="autor" />
    	<input id="searchsubmit" value="Search" type="submit">
    </form>
    
    <!-- Year Search
    So far, I think this is the worst one, don't know exactly how to make it better
    -->
    <form role="search" method="get" id="searchform" action="<?php echo $current_url; ?>">
    	<label for="s">Search by Year:</label>
    	<input type="hidden" value=" " name="s" id="s" id="searchbox" />
    	<select name="year" id="year" class="postform">
    		<option val="0"> — </option>
    		<?php
    		global $post;
    		$query = 'numberposts=-1&category=' . $cat . '&orderby=date&order=DESC';
    		$myposts =  get_posts($query);
    
    		foreach($myposts as $post) {
    			$year = get_the_time('Y');
    			$groups[] = $year;
    		}
    
    		$groups = array_values( array_unique( $groups ) );
    
    		foreach ( $groups as $group ) {
    
    			echo '<option value='.$group,'>'.$group.'</option>';
    
    		} ?>
    	</select>
    	<input type="hidden" name="search-type" value="year" />
    	<input id="searchsubmit" value="Search" type="submit">
    </form>
    
    <!-- After this, generic Loop stuff, nothing fancy -->

    After setting up the forms I needed to change the search.php file queries so it would bring different posts for each form:

    <?php
    // Get the search Keyword
    $keyword = get_search_query();	
    
    if ( isset($_GET['search-type']) ) {
    	$type = $_GET['search-type'];
    
    	if ( $type == 'normal' ) {
    
    		// Normal search within category or subcategory from dropdown
    
    	} elseif ( $type == 'tag' ) {
    
                    // Tag search
    
    		echo '<h1>Tag</h1>';
    		$args = array( 'tag' => $keyword );
    		$args = array_merge( $args, $wp_query->query );
                    // unset 's' var, to avoid a 'general' search
    		unset($args['s']);
    		query_posts( $args );
    
    		/*
    		// Output the query vars used
    		echo '<pre>';
    		print_r($wp_query->query_vars);
    		echo '</pre>';
                    */
    
    	} elseif ( $type == 'autor' ) {
    
                    // Custom Field 'autor' search
    
    		echo '<h1>Autor</h1>';
    
    		$args = array(
    			'meta_query' => array(
    				array(
    					'key' => 'autor',
    					'value' => $keyword,
    					'compare' => 'LIKE'
    		)	)	);
    
    		$args = array_merge( $args, $wp_query->query );
    		// unset 's' var, to avoid a 'general' search
                    unset($args['s']);
    		query_posts( $args );
    
    		/*
    		// Output the query vars used
    		echo '<pre>';
    		print_r($wp_query->query_vars);
    		echo '</pre>';
                    */
    
        } elseif ( $type == 'year' ) {
    
    		// Year search
                    echo '<h1>Year</h1>';
    
    		/*
                    // I didin't need this stuff, the form passes the year var straight to the query.
    
    		$year = $_GET['year'];
    		echo $year;
    
    		$args = array( 'year' => $keyword );
    		$args = array_merge( $args, $wp_query->query );
    		query_posts( $args );
    
    		// Output the query vars used
    		echo '<pre>';
    		print_r($wp_query->query_vars);
    		echo '</pre>';
    		*/
    
        }
    }
    
    // After this, generic Loop stuff, nothing fancy ?>

Topic Closed

This topic has been closed to new replies.

About this Topic