• WPChina

    (@wordpresschina)


    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!

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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; ?>"

    Thread Starter WPChina

    (@wordpresschina)

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

    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.

    Thread Starter WPChina

    (@wordpresschina)

    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 ??

    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.

    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.

    Thread Starter WPChina

    (@wordpresschina)

    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?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘I need a plugin that will only search certain categories’ is closed to new replies.