Support » Plugins » Hacks » Echo taxonomy terms in get_search_query

  • I’ve got a multi term search set up on my siteL

    <form method="get" action="<?php bloginfo('url'); ?>">
    
        <div class="search-text">
          <input type="text" name="s" id="advanced-search" value="<?php if(is_search()) { the_search_query(); } ?>" />
          <a href="#" id="clear-advanced-search">Clear</a>
        </div>
    
        <div class="search-field filter">
          <div class="trigger">Field<span></span></div>
          <div class="options">
            <?php
              $fieldargs = array(
                'orderby' => 'name',
                'hide_empty' => true,
              );
          		$fieldtags = get_terms( 'job-field', $fieldargs );
          		$fieldcheckboxes = '';
          		foreach($fieldtags as $fieldtag) :
          			$fieldcheckboxes .='<input type="checkbox" name="job-field[]" value="'.$fieldtag -> slug.'" id="tag-'.$fieldtag->term_id.'" /><label for="tag-'.$fieldtag->term_id.'">'.$fieldtag->name.'</label>';
          		endforeach;
          		print $fieldcheckboxes;
          	?>
          </div>
        </div>
    
        <div class="search-position filter">
          <div class="trigger">Position<span></span></div>
          <div class="options">
            <?php
              $positionargs = array(
                'orderby' => 'name',
                'hide_empty' => true,
              );
          		$positiontags = get_terms( 'job-position', $positionargs );
          		$positioncheckboxes = '';
          		foreach($positiontags as $positiontag) :
          			$positioncheckboxes .='<input type="checkbox" name="job-position[]" value="'.$positiontag -> slug.'" id="tag-'.$positiontag->term_id.'" /><label for="tag-'.$positiontag->term_id.'">'.$positiontag->name.'</label>';
          		endforeach;
          		print $positioncheckboxes;
          	?>
          </div>
        </div>
    
        <div class="search-location filter">
          <div class="trigger">City<span></span></div>
          <div class="options">
            <?php
              $cityargs = array(
                'orderby' => 'name',
                'hide_empty' => true,
              );
          		$citytags = get_terms( 'job-city', $cityargs );
          		$citycheckboxes = '';
          		foreach($citytags as $citytag) :
          			$citycheckboxes .='<input type="checkbox" name="job-city[]" value="'.$citytag -> slug.'" id="tag-'.$citytag->term_id.'" /><label for="tag-'.$citytag->term_id.'">'.$citytag->name.'</label>';
          		endforeach;
          		print $citycheckboxes;
          	?>
          </div>
        </div>
    
        <input class="button" type="submit" value="Refine Search" />
        <input type="hidden" name="post_type" value="job" />
      </form>

    The Search is working, but I’n not having any luck echoing out the custom term names from the checkboxes when using

    get_search_query( false )

    Any ideas?

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

    (@bcworkz)

    I’m not following your logic. How can you get search query terms when the search form has not yet been submitted? Maybe it doesn’t matter, I’ll try to answer anyway.

    You can get the checkbox values from $_GET or $_SERVER, they will not be in a format to use with the usual ‘s=search-terms’. You could submit your form to a page which is based on a custom template that contains the necessary code to take the checkbox values and run a query directly, or redirect after rearranging the values into the ‘s=search-terms’ format.

    If you don’t like the custom page template idea, the form could also submit to admin-post.php or use AJAX to cause the necessary code to execute.

    Apologies – my initial question wasn’t very clear.

    What I’m wanting is, when someone does a search using the form above, I’d like to echo out the names of any taxonomies the user has selected on the search results page e.g. ‘5 results for “Term, City name, Job Type name”‘ as currently it will only give me the search term from the regular text input search.

    Moderator bcworkz

    (@bcworkz)

    Ah! I see now, thanks for clarifying. I believe my answer is still appropriate, if a bit scattered. While the GET method will work, you might consider switching to POST, it’s not as messy. Naturally, the form data will then be in $_POST. It also sounds like a custom template based search results page is all the more appropriate though the other options are still possible.

    The template code will collect the form data, format it into an appropriate WP_Query arguments array, then execute the query. The results can then be output directly on the page through a standard archive page loop after outputting the user’s selections at the top.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Echo taxonomy terms in get_search_query’ is closed to new replies.