I have a website with some custom post type created with Ultimate CMS plugin.
In admin area, when I make a new search, the result is ok, but after that, when I’m try to make a second search, It give me an error “Invalid post type”.
I also realize that the url it’s not ok:
In the first search, the url is something like this:
http://www.site.com/wp-admin/edit.php?s=SearchTerm1&post_status=all&post_type=post&action=-1&m=0&cat=0&paged=1&mode=list&action2=-1
In the second search, the url is something like:
And the error message: “Invalid post type”.
I deactived all of my plugins, I upgraded wordpress to the latest version 3.5.1, I reset permalinks to default, but this error still remain.
Any help would be much appreciated!
Thank you
I also came across this issue and found that it was a result of one of my functions in my functions.php file that was modifying the global wordpress query parameters.
It sounds like you edited the global
$query
object. If you used a hook such as ‘pre_get_posts’ and manipulated the$query
object and you do not exclude the admin area, then any changes you make to the query parameters will also apply to the admin panel and it will display that error when trying to add in parameters that don’t fit your search.For example:
Let’s say you have a search feature on your site, and when the user enters a search and goes to the search results page, you only want to display posts of a custom post type called
$searchable_posts
, then you would add a hook to yourfunctions.php
file like this:This will make it so that any global default
$query
will only search for results that have a matching post type of$searchable_posts
. However, the way it is written above means this will also apply to any global$query
in the admin panel also.The way around this is to structure your query like this:
Adding in that
!is_admin()
means that your function will exclude anything in the backend administration panel (see is_admin ).Alternatively, a safer way if you can, instead of using the global default
$query
is to create your own new WP_Query and searching using that instead – the codex has good examples of how to set that up.I am also get the issue same as you and finally I got the solution.
We have to add
$query->is_main_query()
in IF CONDITION.Below is the full code.
I had a similar problem with a site a took over from other dev.
Someone forgot to exclude this function from wp-admin page. So changing to this helped Invalid post type.
At wp-admin when submitted the second search for a custom post type, I was getting the same error, “Invalid post type.” The URL seemed long and incorrect. Anyway, the following code worked perfectly: