WordPress.org

Ready to get started?Download WordPress

Forums

I need a plugin that will only search certain categories (8 posts)

  1. WPChina
    Member
    Posted 6 years ago #

    I have 5 main categories in my blog and then within each of those categories I have 5-20 sub-categories. I want to create a search form on each of those 5 top-level category pages that ONLY search that category and its sub-category (child) categories.

    I tried the Search Everything plugin but it does not fulfill this need.

    does anyone know of a good plugin that I can customize (manually or via the admin panel) to allow me to search specific categories? I will then create separate search forms for each category and have them all work on the site... the SE plugin above only allows me to use the search form once, so I can't then customize it for all my other category trees.

    Tks!

  2. Kafkaesqui

    Posted 6 years ago #

    Don't know of a plugin offhand which does or could be modified for this, but the following code will do pretty much what you ask:

    <?php
    if( is_category() ) :
    	$category = $wp_query->get_queried_object();
    	$cat_parent = ( $category->category_parent ) ? $category->category_parent : $category->cat_ID;
    	$action_url = get_category_link($cat_parent);
    else :
    	$action_url = get_bloginfo('home');
    	if( is_search() ) :
    		$action_url .= $_SERVER['REQUEST_URI'];
    	endif;
    endif;
    ?>

    Then merely change the action attribute in your search form to:

    action="<?php echo $action_url; ?>"
  3. WPChina
    Member
    Posted 6 years ago #

    Does that go right next to my <form></form> code for my searchform? Tks!

  4. Kafkaesqui

    Posted 6 years ago #

    I'd place the code statement just before the search <form>. Much for sanity's sake so anyone coming to it later can figure out the purpose of $action_url and such.

    EDIT: Realized I didn't explain what's happening here, which is useful to know. On a WP blog, adding the search query string (i.e. ?s=search) on the end of the url for any archival query type, whether that be category or dated archives or author, forces a search on that particular query only. So:

    blog/?s=search

    Searches an entire blog, whereas:

    blog/category/world/?s=search

    Searches only under the 'World' category of that blog.

    My code assigns the parent category url to $action_url when on a category query (or the current URL in case additional searches are initiated from the search results page). This is then assigned as the value to the action attribute of the search form, which WordPress uses for the search URI endpoint.

  5. WPChina
    Member
    Posted 6 years ago #

    Ahhh!! F****ing amazing! I had no idea blog/category/world/?s=search could render a search only within a category. WP is so extensible! :)

    Ok, I will tinker with this now.

    Because.... now I see how I an create a dropdown window to always be at the header of the blog and then the person drops down the category they want and it renders a search for them... amazing... sorry, I'm sure many other people are smarter than me and already knew this, but I find this now so easy...

    I'm glad I moved from MT to WP 2 years ago :D

  6. Roy
    Member
    Posted 6 years ago #

    Does this also work for blog/the_title/?s=search or something??

    I've been searching for a way to search post titles only (preferably optional with a checkbox or dropdown) and this seems to be a way to look into further.

  7. Kafkaesqui

    Posted 6 years ago #

    No, it would not change what WordPress searches on, that is limiting it to some element of a post like the title. For that you're looking at a mod to the source or a plugin which provides such a restriction.

  8. WPChina
    Member
    Posted 6 years ago #

    Now that I know I can search specific categories, is there a way to return custom search results pages for each category?

    For example if I search /blog/hobbies/ I want a special search results page for all the /hobbies/. Then if I search /blog/work/ I want a different search results template page for the /work/ results.

    There must be a way to do this, but I can't figure out how... anybody know how to do this?

Topic Closed

This topic has been closed to new replies.

About this Topic