Home › Forums › Support › Change searchform.php to limit search results to within active category archive
- This topic has 9 replies, 2 voices, and was last updated 6 years, 5 months ago by
Tom.
-
AuthorPosts
-
October 30, 2019 at 10:50 am #1049051
Heather
Hi,
I am using the built in search bar on a website, and by default the results it provides are everything related to the searched term.
I have updated my functions.php to allow the filter the results to just posts, but if I am on a category archive page and use the search, it still searches across the entire site.
I want to filter the search results to only relevant results based on the category archive I am in. IE: If I am on an archive page for ‘happy’, and I want to search for posts about ‘sun’, when I search ‘sun’, I do not want ALL posts with sun from across the entire site, I ONLY want results about ‘sun’ in the category of ‘happy’
I tried changing the searchform.php file to have category filters, but it is not working, do I need to add some GP specific terms in here to make it function properly? Find code below:
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>"> <input type="hidden" name="post_type" value="post"> <?php if ( is_category() || is_tag() ) { ?> <?php $current_category = get_queried_object(); ?> <label> <span class="screen-reader-text"><?php echo esc_html_e( 'Search for:', 'client-name' ); ?></span> <input type="search" class="search-field" placeholder="<?php echo esc_html_e( 'Search', 'client-name' ); ?> <?php echo $current_category->name; ?>" value="<?php echo get_search_query() ?>" name="s" required/> </label> <?php if ( is_category() ) { ?> <input type="hidden" name="category_name" value="<?php echo $current_category->slug ?>"> <?php } elseif ( is_tag() ) { ?> <input type="hidden" name="tag" value="<?php echo $current_category->slug ?>"> <?php } } else { ?> <label> <span class="screen-reader-text"><?php echo esc_html_e( 'Search for:', 'client-name' ); ?></span> <input type="search" class="search-field" placeholder="<?php echo esc_html_e( 'Search', 'client-name' ); ?>" value="<?php echo get_search_query() ?>" name="s" required/> </label> <?php } ?> <input type="submit" class="search-submit" value="<?php echo apply_filters( 'generate_search_button', _x( 'Search', 'submit button', 'generatepress' ) ); // WPCS: XSS ok, sanitization ok. ?>"> </form>October 30, 2019 at 11:01 am #1049062Heather
Also, the Search input placeholder text does not disappear on focus, the word search is still there and I have to backspace to delete the word before I can search.
How do I make this disappear on focus?October 30, 2019 at 3:55 pm #1049223Tom
Lead DeveloperLead DeveloperHmm, I don’t have much experience with this. What if you use
catinstead ofcategory_name?October 31, 2019 at 9:04 am #1049809Heather
I tried that and it didn’t work unfortunately. I am bringing in the search form/search bar via a short code, and that shortcode does have a form in it as well. This is the code:
function wpbsearchform( $form ) { $form = '<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" > <div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label> <input type="text" value="Search' . get_search_query() . '" name="s" id="s" /> <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" /> </div> </form>'; return $form; } add_shortcode('wpbsearch', 'wpbsearchform');If I try to add the form above, I get an error because of the the values between ‘ ‘ don’t get read (see photo)
Do you know if there is a filter I can add to the form in this shortcode that will limit my search to just the archive page I am on? some sort of dynamic boolean statement like if cat = cat_id do this else do this?
Thanks
proper function for shortcode:

updated form shortcode with error in functions.php:
October 31, 2019 at 8:50 pm #1050196Tom
Lead DeveloperLead DeveloperTry this:
add_shortcode( 'wpbsearch', function() { ob_start(); ?> <form role.... etc..> <?php return ob_get_clean(); } );I’ve looked around for how to limit results to the current category, but I can’t find anything for some reason. Have you looked into a plugin like this?: https://wordpress.org/plugins/relevanssi/
Maybe it has an option for that?
November 1, 2019 at 8:09 am #1050573Heather
Hi,
That code worked with a bit of tinkering! Thanks. I am running into a new issue where even though the search is now limiting it’s search to the category of the archive it is on, when the search button is pressed, it brings up all the posts from that category regardless of the term searched. So even if you search for a very specific term, it just brings up everything with that category. This is the updated code
Also thanks for the plugin recommendation, we are trying to make the website as lightweight as possible so we would like to avoid plugins for now but will look into it if this doesn’t workadd_shortcode( 'wpbsearch', function() { ob_start(); ?> <form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>"> <input type="hidden" name="post_type" value="post"> <?php if ( is_category() || is_tag() ) { ?> <?php $current_category = get_queried_object(); ?> <label> <span class="screen-reader-text"><?php echo esc_html_e( 'Search for:', 'client-name' ); ?></span> <input type="search" class="search-field" placeholder="<?php echo esc_html_e( 'Search', 'client-name' ); ?> <?php echo $current_category->name; ?>" value="<?php echo get_search_query() ?>" name="s" required/> </label> <?php if ( is_category() ) { ?> <input type="hidden" name="category_name" value="<?php echo $current_category->slug ?>"> <?php } elseif ( is_tag() ) { ?> <input type="hidden" name="tag" value="<?php echo $current_category->slug ?>"> <?php } else { ?> <label> <span class="screen-reader-text"><?php echo esc_html_e( 'Search for:', 'client-name' ); ?></span> <input type="search" class="search-field" placeholder="<?php echo esc_html_e( 'Search', 'client-name' ); ?>" value="<?php echo get_search_query() ?>" name="s" required/> </label> <?php } ?> <button type="submit" class="search-submit" value="" /> <span class="screen-reader-text"><?php echo esc_html_e( 'Search', 'client-name' ) ?></span> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 23.92 23.93" aria-hidden="true" class="search-submit-icon"><path d="M17,14.69l.39.14a2.51,2.51,0,0,1,.8.61l5.07,5.07a1.93,1.93,0,0,1,.57,1.83,1.89,1.89,0,0,1-1,1.33,1.91,1.91,0,0,1-2-.07,2.56,2.56,0,0,1-.37-.3l-5.24-5.23a2.15,2.15,0,0,1-.53-.88.93.93,0,0,0,0-.13,9.33,9.33,0,0,1-9.57.63,8.83,8.83,0,0,1-4.08-4A9.35,9.35,0,0,1,14.5,1.55a9,9,0,0,1,4,6A9.25,9.25,0,0,1,17,14.69ZM9.35,16.35a7,7,0,1,0-7-7A7,7,0,0,0,9.35,16.35Z"/></svg> </button> <?php return ob_get_clean(); } } );November 1, 2019 at 8:35 am #1050590Heather
It also seems now that if it is not on a category archive page, the search bar does not show up via the shortcode. So it used to be on the blog archive page/posts page, and now it is gone, but it appears if I am in a category archive.
Not sure why that is happening as the code seems like it should still be able to be pulled in via shortcode anywhere.
November 2, 2019 at 12:30 am #1051086Tom
Lead DeveloperLead DeveloperHi there,
I’ll need to implement the code on a test site to know what’s going on, unfortunately. It’s kind of a complex thing that doesn’t seem to have an answer that I can find.
I’ll try to find time to play with it on Monday. I’m not 100% sure I’ll be able to come up with a solution, so you might need to find a developer to spend a couple of hours on it. Codeable.io is worth checking out if you need to go that route.
November 2, 2019 at 9:04 am #1051432Heather
Ok, thank you! I will check back in Monday/Tuesday. I will check out the codeable.io in the meantime. I’ve tried to research it quite a bit and it seems to be a bit difficult, so I appreciate you taking the time to test it
November 5, 2019 at 9:53 am #1054108Tom
Lead DeveloperLead DeveloperI’m afraid I couldn’t get this to work within a reasonable amount of time – I’m sorry!
You could try asking over on https://wordpress.stackexchange.com/.
Someone might have the correct code handy 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.