Search in Archive Pages by subcategory, tag, custom field and year

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.

Read More

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.

Forget about multiple forms….

UPDATE

FORM:

<?php
// Current URL for setting up Search Form Action
global $wp;
$current_url = get_home_url(null, $wp->request, null);
?>

<form role="search" method="get" action="<?php echo $current_url; ?>/">
<input type="hidden" name="s" value="true" />

<!-- General Search -->
<label for="s">General</label>
<input type="text" name="s" />

<!-- Tag Search -->
<label for="tag">Tag</label>
<input type="text" name="tag" />

<!-- Autor Search -->
<label for="autor">Autor</label>
<input type="text" name="autor" />

<input value="Buscar" type="submit">
</form>

FUNCTION

// Extend Search
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {

        $query->set( 'tag', $query->query_vars['s'] );

        $query->set( 'meta_key' , 'autor' );
        $query->set( 'meta_value' , $query->query_vars['s'] );

}   }   }
add_action('pre_get_posts','search_filter');

Related posts

2 comments

  1. You only need one form and a callback on pre_get_posts. For example:

    <form role="search" method="get" id="searchform" action="<?php echo $current_url; ?>">
        <input type="hidden" name="s" value="true" />
    
        <!-- Tag Search -->
        <label for="tags">Search Tag:</label>
        <input type="text" value="Search" name="tags" id="tagsearchbox" />
    
        <!-- Custom Field "autor" Search -->
        <label for="autors">Search in Custom Field 'autor':</label>
        <input type="text" value="Search" name="autors" id="autorsearchbox" />
    
        <input id="searchsubmit" value="Search" type="submit">
    </form>
    

    Now your callback:

    function alter_search_ppp_wpse_107154($qry) {
      if ($qry_>is_main_query() && $qry->is_search()) {
        // parse your fields here and alter the query with $qry->set like this :
        $qry->set('post_per_page',10);
      }
    }
    add_action('pre_get_posts','alter_search_ppp_wpse_107154');
    

    You should validate that code. You have strange things happening, like multiple id attributes on some elements and put that Javascript in a file and enqueue it.

  2. In case it comes handy for anyone.

    My goal was to inlcude an enhaced/better search, providing other fields for tag and custom field search.

    The following form is inserted in archive.php, $categoria_id being the current archive category:

    <form class="group" role="search" method="get" action="<?php echo site_url(); ?>/">
        <input type="hidden" name="cat" value="<?php echo $categoria_id; ?>" />
    
        <!-- General Search -->
        <input type="text" value="General" name="s" onfocus="if (this.value == 'General') {this.value = '';}" onblur="if (this.value == '') {this.value = 'General';}" />
        <!-- Tag Search -->
        <input type="text" value="Etiqueta" name="tags" onfocus="if (this.value == 'Etiqueta') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Etiqueta';}" />
        <!-- Autor Search -->
        <input type="text" value="Autor" name="autor" onfocus="if (this.value == 'Autor') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Autor';}" />
    
        <input value="Buscar" type="submit">
    </form>
    

    In functions.php I inserted the following function, as suggested by @s_ha_dum

    // Extend Search
    function search_filter($query) {
        if ( !is_admin() && $query->is_main_query() ) {
            if ( $query->is_search ) {
    
                $tags = $_GET['tags'];
                if ( isset( $_GET['tags'] ) && $_GET['tags'] == 'Etiqueta' ) {
                    $tags = NULL;
                } elseif ( $tags != NULL ) {
                    $query->set( 'tag', $tags );
                }
    
                $autor = $_GET['autor'];
                if ( isset( $_GET['autor'] ) && $_GET['autor'] == 'Autor' ) {
                    $autor = NULL;
                } elseif ( $autor != NULL ) {
                    $metaQuery = array( array( 'key' => 'autor', 'value' => $autor, 'compare' => 'LIKE' ) );
                    $query->set( 'meta_query' , $metaQuery );
                }
    
    }   }   }
    add_action('pre_get_posts','search_filter');
    

Comments are closed.