WordPress.org

Ready to get started?Download WordPress

Forums

Searching with multiple tags using checkboxes (12 posts)

  1. tta
    Member
    Posted 3 years ago #

    Hello,

    I've googled some but I haven't really found any solution.

    I wish to be able to search my wordpress site using multiple tags, not only 1 tag per search.
    So it'd be a search field, and then the tags are listed with a checkbox next to them.

    I have some code in my search field that lists all the tags in use, and puts checkboxes next to them. But right now, when checking multiple tags, it only takes one of them and does a search for it.

    How would I go about to make it do a search for tag1,tag2 or tag1+tag2? (or both, by choice)

    Thanks a lot.

    Incomplete code, found somewhere earlier on the net:

    <?php
    	$tags = get_terms( 'post_tag' );
    	$checkboxes = '';
    	foreach($tags as $tag) :
    		$checkboxes .=
    		'<label for="tag-'.$tag->term_id.'">'.$tag->name.'
    			<input type="checkbox" name="tag" value="'.$tag -> slug.'" id="tag-'.$tag->term_id.'" /><br />
    		</label>';
    	endforeach;
    	print $checkboxes;
    	?>
  2. rot8tor
    Member
    Posted 3 years ago #

    I'm trying to do this same thing....and haven't been able to solve it either...can I ask you to share the complete form code that does the one at a time?

  3. tta
    Member
    Posted 3 years ago #

    Hey,

    there's a plugin called Enhanced Search Form, and supposedly it lets you search with tags and categories. I didn't get it to work (on 3.0.1) though. See if you can get it to work.

    Other than that, I did manage to make something that worked for me. No idea how good it actually is, keep in mind that I am not really a php coder. Someone could probably come up with a better solution, no doubt.

    You have a search form and you can search for a keyword, tags (check mutliple ones) and a category. You can choose to search for posts containing ALL tags or ANY of the chosen tags.

    So the search form POSTs your choices to this other file, which will build a search URL for you and send you to it.

    So try this out, at your own risk. :)

    searchform.php:

    <form action="<?php bloginfo('template_url') ?>/build_search.php" method="post" accept-charset="utf-8">
    	<div>Search</div>
    	<input type="text" name="keywordsearch" value="" id="title">
    
    	<div>Category</div>
    	<?php wp_dropdown_categories( 'show_option_all=All Categories' ); ?>
    
    	<div>Tags</div>
    	<select name="and-or" id="select-and-or">
    		<option value="OR">Match ANY of the checkboxes (default)</option>
    		<option value="AND">Match ALL of the checkboxes</option>
    	</select>
    	<div>
    		<?php
    		// The following will list all tags with a checkbox next to them.
    		$tags = get_terms( 'post_tag' );
    		$checkboxes = '';
    		foreach($tags as $tag) :
    			$checkboxes .='<input type="checkbox" name="tag[]" value="'.$tag -> slug.'" id="tag-'.$tag->term_id.'" /><label for="tag-'.$tag->term_id.'">'.$tag->name.'</label>';
    		endforeach;
    		print $checkboxes;
    		?>
    	</div>
    	<p><input type="submit" value="Search"></p>
    </form>

    Now the following file, there's a lot you probably could do to shorten it, but as I mentioned I'm not really a php coder and I just typed stuff up to make it happen for me. :)
    build_search.php:

    <?php
    
    	//	Keyword search
    	if (isset($_POST['keywordsearch'])) {
            $text_search = $_POST['keywordsearch'];
            $text = $text_search;
    	}
    
    	//	If there is no keyword entered
    	else {
            $text = '';
    	}
    
    	//	If there's a category search
    	if (isset($_POST["cat"])) {
    		$cat = $_POST["cat"];
    	}
    
    	// If there's a tag search
    	if (isset($_POST["tag"])){
    		$tags_array = array();
    		$tags_array = $_POST["tag"];
    
    		foreach ($tags_array as $key => $value)
    		{
    			if ($_POST["and-or"] == "OR") {
    				//	tag1 OR tag2 is chosen, add a comma after the tags
    				$string .= $value.',';
    			}
    			elseif ($_POST["and-or"] == "AND") {
    				//	tag1 AND tag2 is chosen, add a plus after the tags.
    				$string .= $value.'+';
    			}
    		}
    
    		// Remove the last symbol in the string, in this case the comma or plus that is added after each tag. We don't want it to look like "tag1+tag2+".
    		$tags_string = substr($string, 0, -1);
    
    			$tag = $tags_string;
    	}
    
    	//	If there's no search for tags, set it to 0
    	else {
    		$tag = 0;
    	}
    
    	// build the url with the variables
    	$url = header("Location:/fv/?s=$text&cat=$cat&tag=$tag");
    ?>

    then the file that presents your search results,
    search.php

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <div>Title</div>
    <?php the_title(); ?>
    
    <div>Excerpt</div>
    <?php the_excerpt(); ?>
    
    <div>Tags</div>
    <?php the_tags('',','); ?>
    
    <div>Category</div>
    <?php the_category(); ?>
    
    <?php endwhile; else: ?>
    <p><?php _e('Nothing found.'); ?></p>
    <?php endif; ?>

    Hope it works for ya, worked for me.

  4. rot8tor
    Member
    Posted 3 years ago #

    thanks a bunch, I'll give it a try...

  5. curtislow
    Member
    Posted 3 years ago #

    Thanks tykho! Exactly what I was looking for. Someone should really turn this into a plugin. Lots of people are searching for something like this. Kind regards

  6. msanguinetti
    Member
    Posted 3 years ago #

    Thanks for posting Tykho. Your search function is exactly what I would like to add to my blog but I am having trouble with the execution.

    I am a novice, but I can follow the searchform.php and build-search.php functions. I placed the searchform.php code in my sidebar to generate the search form input. My problem lies in translating the search credentials into an accurate results listing. Something is missing between the input and output. Currently, when I run a search, it takes me to a 404 page.

    Am I missing a piece of the puzzle that ties everything together?

  7. curtislow
    Member
    Posted 3 years ago #

    Hi msanguinetti,

    I had the same problem. I noticed the strange url ($url = header("Location:/fv/?s=$text&cat=$cat&tag=$tag")) at the bottom of build_search.php. That seemed to be the problem. I removed de "/fv" at the start of the url and that seemed to do the trick for me.

    $url = header("Location:/?s=$text&cat=$cat&tag=$tag");

    I am not sure why the "/fv" part was in the url, but maybe tykho has an explanation for this.

    Good luck!

  8. tta
    Member
    Posted 3 years ago #

    Hey guys,

    Yes, curtislow is right, sorry about the /fv part. I forgot to edit that out, it's from a local development site when I wrote that code. :)

  9. msanguinetti
    Member
    Posted 3 years ago #

    That solved it! Thank you so, so much. Now I have lots of styling work to do and it will be perfect!

  10. curtislow
    Member
    Posted 3 years ago #

    Hi tykho,

    I try to display a title for the search results (You searched for:). I added the following line to search.php: printf( __( 'Search Results for: %s' ), '' . get_search_query() . '' )

    It seems that this only works for the keyword search and not for categories and tags. Have you got any advice on this one? I would be very grateful!

  11. curtislow
    Member
    Posted 3 years ago #

    Hi tykho,

    I used the solution you offered above using searchform.php, build_search and search.php. After upgrading to WordPress 3.1 keyword search is broken. Did you notice? And do you have a solution? I spent hours on it already, but without succes.

    Kind regards

  12. bezbeli
    Member
    Posted 3 years ago #

    This should be turned to plugin definitely :-) thank you very much Tykho. Is it possible to have posts sorted by closest match below the actual results, or if there is no perfect match?

    Thanx a lot!!!

Topic Closed

This topic has been closed to new replies.

About this Topic