• 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.

Viewing 15 replies - 1 through 15 (of 20 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Thread Starter duncmorley

    (@duncmorley)

    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?

    Moderator keesiemeijer

    (@keesiemeijer)

    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.

    Thread Starter duncmorley

    (@duncmorley)

    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.

    Moderator keesiemeijer

    (@keesiemeijer)

    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).

    Thread Starter duncmorley

    (@duncmorley)

    Are you suggesting that shoud’ve worked then?

    Moderator keesiemeijer

    (@keesiemeijer)

    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.

    Thread Starter duncmorley

    (@duncmorley)

    Moderator keesiemeijer

    (@keesiemeijer)

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

    Thread Starter duncmorley

    (@duncmorley)

    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?

    Moderator keesiemeijer

    (@keesiemeijer)

    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.

    Thread Starter duncmorley

    (@duncmorley)

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

    You’ll notice there is one instance of:

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

    Moderator keesiemeijer

    (@keesiemeijer)

    I see.

    I wonder why the others worked without this.

    Maybe because of this:

    if (!$post_type) {
      $post_type = 'any';
    }

    Thread Starter duncmorley

    (@duncmorley)

    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?

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Multiple search boxes on one site’ is closed to new replies.