WordPress.org

Ready to get started?Download WordPress

Forums

Multiple search boxes on one site (21 posts)

  1. duncmorley
    Member
    Posted 2 years ago #

    Hi

    I would like to have 2 search widgets on my website. I would like one to search my pages and one to search my posts. I've found lots of documentation on the web which helps you do one or the other but nothing where you can have 2 on the same site. Any help with this would be great.

  2. keesiemeijer
    moderator
    Posted 2 years ago #

  3. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

  4. duncmorley
    Member
    Posted 2 years ago #

    Keesiemeijer, this line seemed to work great for posts & custom post types.

    <input type="hidden" name="post_type" value="page" />

    I am now creating a search plugin where you can select you desired post type from the widget.

    My only issue at the minute is that posts & custom post types are being searched when using page as the value. Surely this is incorrect?

  5. keesiemeijer
    moderator
    Posted 2 years ago #

    if I use this (for testing):

    function mySearchFilter($query) {
    	$post_type = 'page'; // I CHANGED this for testing
    	if (!$post_type) {
    		$post_type = 'any';
    	}
        if ($query->is_search) {
            $query->set('post_type', $post_type);
        };
        return $query;
    };
    
    add_filter('pre_get_posts','mySearchFilter');

    It only gets search results from Pages.

  6. duncmorley
    Member
    Posted 2 years ago #

    I'm not sure this solves my issue. I need 3 search forms throughout my website. One for pages, one for posts and one for my custom post type.

    Therefore i have created 3 forms, each form has a different hidden field (see below).

    Page
    <input type="hidden" name="post_type" value="page" />

    Post
    <input type="hidden" name="post_type" value="post" />

    Custom Post Type
    <input type="hidden" name="post_type" value="case-studies" />

    This works great apart from Page which returns all results instead of just pages.

  7. keesiemeijer
    moderator
    Posted 2 years ago #

    Have you tried:
    - deactivating all plugins to see if this resolves the problem? If this works, re-activate the plugins one by one until you find the problematic plugin(s).

  8. duncmorley
    Member
    Posted 2 years ago #

    Are you suggesting that shoud've worked then?

  9. keesiemeijer
    moderator
    Posted 2 years ago #

    Yes If I do $post_type = 'page'; // I CHANGED this for testing I get only "Pages" search results.
    If I do $post_type = 'post'; // I CHANGED this for testing I get only "Posts" search results.

    Can you paste and submit the full code with the forms into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

    Then I can test it with your code.

  10. duncmorley
    Member
    Posted 2 years ago #

  11. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with: $post_type = $_GET['post_type']; in stead of $post_type = $_GET['type']; in your functions.php

  12. duncmorley
    Member
    Posted 2 years ago #

    That's worked great, thanks. I wonder why the others worked without this. If I wanted to put this function within my plugin instead of my functions.php file, where about's would I put it?

  13. keesiemeijer
    moderator
    Posted 2 years ago #

    Maybe because the other forms had this in it:

    <input type="hidden" name="type" value="page" />

    Check if they work now that you made the alteration in functions.php
    The thing to look for is this name="type", change it in all three forms to name="post_type":

    <input type="hidden" name="post_type" value="[post-page-custom]" />

    and change [post-page-custom] to the right post type.

  14. duncmorley
    Member
    Posted 2 years ago #

    Take a look at the plugin i'm trying to create: http://pastebin.com/vkiXpCbw

    You'll notice there is one instance of:

    <input type="hidden" name="post_type" value="[post-page-custom]" />

  15. keesiemeijer
    moderator
    Posted 2 years ago #

    I see.

    I wonder why the others worked without this.

    Maybe because of this:

    if (!$post_type) {
      $post_type = 'any';
    }
  16. duncmorley
    Member
    Posted 2 years ago #

    I didn't have the function that included that when it wasn't working. I now have this in my functions.php file:

    function SearchFilter($query) {
    	$post_type = $_GET['post_type'];
        if ($query->is_search) {
            $query->set('post_type', $post_type);
        };
        return $query;
    };
    
    add_filter('pre_get_posts','SearchFilter');

    Anyidea where or if i can include this in my plugin?

  17. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with this: http://pastebin.com/a2vPrSk2
    I've commented my contributions with // my code

  18. duncmorley
    Member
    Posted 2 years ago #

    That works like a treat. Thank you very much for all you help with this.

    Do you think this plugin would be worth submitting. From what I could see there wasn't anything out there that did what this does... but I may be wrong.

  19. keesiemeijer
    moderator
    Posted 2 years ago #

    I would test it more thoroughly before submitting. and maybe change this

    // my code 1
    add_filter('pre_get_posts', array($this,'mySearchFilter'));

    to:

    // my code 1
    if(isset($_GET['post_type']) && $_GET['post_type']) {
    add_filter('pre_get_posts', array($this,'mySearchFilter'));
    }

    So you would only add a filter to the query when there was a $_GET['post_type'] submitted.

    I would also prefix it so you don't confllict with other plugins widgets: "post_type" to "spt_post_type".

  20. mattspaeth
    Member
    Posted 2 years ago #

    This is awesome. I have been looking to do the exact same thing. Did you submit this plugin? I would love to use it.

  21. Cristiano Carletti
    Member
    Posted 1 year ago #

Topic Closed

This topic has been closed to new replies.

About this Topic