WordPress.org

Ready to get started?Download WordPress

Forums

Categories within search... (16 posts)

  1. noelgreen
    Member
    Posted 5 years ago #

    So, back to the CouponFridge site. If you go to the main page and type "Dallas" into the search, then click "View Coupons" you'll see at least one coupon come up on the sub-page. You will also see tabs appear at the top for categories like, "Auto & Transportation," "Entertainment & Electronics," and so on. But, if you click on one of those it only shows you that entire category.

    My question is, how can I make it so that once you search for a city, such as "Dallas," and then click on a tab, such as "Restaurants & Delivery," that it will only show the things posted in the restaurant & delivery category WITHIN that search?

    If that's not clear please ask questions.

  2. noelgreen
    Member
    Posted 5 years ago #

    My category links are hard-coded into the page to so that they each have their own style assigned to them. So if you answer, keep that in mind.

  3. noelgreen
    Member
    Posted 5 years ago #

    A few options I thought of...

    1) I could make the links to the categories actually search buttons that are text. So when someone clicks on one of them they're taken to a search results page that includes only those categories.

    2) Make separate blogs for each city. So Dallas would be http://www.couponfridge.com/dallas and so on. Then people could enter in their city and it would take them to that specific blog.

    The only problem is, I don't know how to really do either. With option #2 I don't want to install a blog for every city. Is there an easy way to do this? Are there any other thoughts out there? Otto?! Moshu?!?! :D

  4. noelgreen
    Member
    Posted 5 years ago #

    Ba-da-bump

  5. adam-s
    Member
    Posted 5 years ago #

    I think first you would need to add category search ability to your blog via plugin or something. From there you would need to add PHP code that tacks whatever search info onto your tabs.

    Not very helpful, I know. Hopefully a start in the right direction though.

  6. noelgreen
    Member
    Posted 5 years ago #

    Yes, I have the ability to search the categories.

    There has to be a way to search within the category you're currently in. That can't be something TOO hard to do. Is it??

  7. noelgreen
    Member
    Posted 5 years ago #

    No one?

  8. noelgreen
    Member
    Posted 5 years ago #

    This has got to be possible... can anyone help at all?

  9. Mindshare Studios, Inc.
    Member
    Posted 5 years ago #

    If you want to search only the current category (and exclude results from pages, showing posts only) you need to edit the following files in your theme:

    First, edit the search.php file. Just above the loop paste the following code:

    <?php
    if ($_GET['cat'] != '') {
    	$posts = query_posts($query_string . '&post_type=post'); // return only posts
    } else {
    	$posts = query_posts($query_string . '&post_type=page'); // return only posts
    }
    ?>

    Next you need to modify your search form to search only the current category. Replace the default form with:

    <?php $category = get_the_category(); ?>
    <form method="get" id="jobsearch" action="<?php bloginfo('home'); ?>/">
    <div><input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
    <input type="hidden" name="cat" value="<?php echo $category[0]->cat_ID; ?>" />
    <input type="submit" id="jobsubmit" value="Search Jobs" />
    </div>
    </form>

    That should do it.

  10. saganaki
    Member
    Posted 5 years ago #

    @ Mindshare, mind if I ask how you would you modify that code to do the following:

    First
    Input: two specified categories
    Output: posts that are in both categories (AND operation)

    Second, can the search be triggered just by clicking on a url, rather than showing the search form (I think that's possible?)

    Third, can the search function just be used to generate a count of the results that would be than the results themselves (eg 7 posts)

    Fourth, could you run one of the categories through a loop to a list of searches the list.

    The result would be a lists like this:

    Example:
    First category= real estate agents
    Second category hierarchy) top level = state, second level=cities

    List generated be:
    State y
    * Posts about real estate agents in city A (7) - click here to see them
    * Posts about real estate agents in city B (2)- click here to see them
    State z
    * Posts about real estate agents in city D (7) - click here to see them
    * Posts about real estate agents in city E (2)- click here to see them

    As a bonus an if function could be included to not display a list item that had zero posts.

  11. Mindshare Studios, Inc.
    Member
    Posted 5 years ago #

    saganaki - just saw this post. you should probably hire someone to do that for you. ;-)

  12. saganaki
    Member
    Posted 5 years ago #

    I wish...going to have to be DIY

  13. saganaki
    Member
    Posted 5 years ago #

    Hmm, after RFTC this could be the answer:

    Multiple Category Handling

    Display posts that are in multiple categories

    query_posts(array('category__and' => array(2,6)));

    This only shows posts that are in both categories 2 and 6. To display posts from either category 2 OR 6, you could use cat as mentioned above, or explicitly specify by using category__in:

    query_posts(array('category__in' => array(2,6)));

    You can also exclude multiple categories this way:

    query_posts(array('category__not_in' => array(2,6)));

  14. design_dolphin
    Member
    Posted 5 years ago #

    <?php
    if ($_GET['cat'] != '') {
    	$posts = query_posts($query_string . '&post_type=post'); // return only posts
    } else {
    	$posts = query_posts($query_string . '&post_type=page'); // return only posts
    }
    ?>
    <input type="hidden" name="cat" value="<?php echo $category[0]->cat_ID; ?>" />

    First off @mindshare thank you for posting the code.

    I ran into the following, and wanted to posts the results and obstacles because I haven't been able to find a solution so far:

    I hardcoded the "input type" value because I noticed that if you get a search not found result the value would dissapear out of the url.
    Hardcoding this value keeps the focus, which I then use to return a "search not found" template file which includes the correct search form again.

    I want to add multiple search forms in various places and have these search in specified areas such as categories. Using for example "in_category" with elseif statements works. However, now the main search takes over the integer from the url... :/ Still haven't figured out that part out.

    So I want to figure out how to get the main search to search through all the posts adn give a specified search results page regardless of the value in the url.

    Using the '<?php echo $category[0]->cat_ID; ?>' could result in a lot less work, but the url integer would also have to be passed on a "no search result found." Maybe a query_posts and/or in combination with an if statement in the search result template would be an option...(?)

  15. design_dolphin
    Member
    Posted 5 years ago #

    o.k. this code allows one to search a category or combinations of categories:

    <div>
    
    <form action="<?php bloginfo('url'); ?>/" method="get">
    
    <p>
    
    <div><input type="text"  name="s" size="40" /></div>
    
    <label class="your_style"><input type="checkbox" name="cat" value="catnumber1, catnumber2, catnumber3"  />Parent Category name 1</label>
    
    <label class="your_style"><input type="checkbox" name="cat" value="catnumber1"  />Child category name 1</label>
    
    <label class="your_style" ><input type="checkbox" name="cat" value="catnumber2" />Child category name 2</label>
    
    <label class="your_style" ><input type="checkbox" name="cat" value="catnumber3"  />Child category name 3</label>
    
    <div><input  class="your_style" type="submit" value="Search" /></div>
    
    </p>
    
    </form>
    </div>

    The url you get for a single category for example is:

    http://www.your-domain.com/?s=your_search_word&cat=your_cat_number

    One could customize the search form with different fields and/ or for example use cat_ID to pass the category variable dynamically.

  16. macreixa
    Member
    Posted 5 years ago #

    Hi, this works fine.
    However, i activated the "Search Everything" plugin that allows to custom fields to be searchable.

    The problem is that now the above code ignores the "cat" condition and outputs all posts that have the search term in the custom field.

    Any ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic