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?
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.
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.
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).
Are you suggesting that shoud’ve worked then?
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.
Try it with: $post_type = $_GET['post_type'];
in stead of $post_type = $_GET['type'];
in your functions.php
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?
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.
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]" />
I see.
I wonder why the others worked without this.
Maybe because of this:
if (!$post_type) {
$post_type = 'any';
}
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?