Wordpress admin search post error - invalid post type
Asked Answered
M

4

6

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:

http://www.site.com/wp-admin/edit.php?s=SearchTerm2&post_status=all&post_type=Array&_wpnonce=4d88f268e4&_wp_http_referer=%2Fwp-admin%2Fedit.php%3Fs%3DSearchTerm1%26post_status%3Dall%26post_type%3Dpost%26action%3D-1%26m%3D0%26cat%3D0%26paged%3D1%26mode%3Dlist%26action2%3D-1&action=-1&m=0&cat=0&paged=1&mode=list&action2=-1

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

Method answered 22/3, 2013 at 15:31 Comment(4)
what is the value of $_GET['post_type'] ?Agna
Same here, looking for solution.Mooneye
Did anyone find a solution for this? I'm experiencing it now :(Regan
Same here, and haven't found a solution yet... =/Calyx
M
7

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 your functions.php file like this:

function searchfilter($query) {

  if ($query->is_search && $query->is_main_query() ) {

      $query->set('post_type', $searchable_posts);
  }
  return $query;
}

add_filter('pre_get_posts', 'searchfilter');

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:

function searchfilter($query) {

  if ($query->is_search && $query->is_main_query() && !is_admin() ) {

      $query->set('post_type', $searchable_posts);
  }
  return $query;
}

add_filter('pre_get_posts', 'searchfilter');

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.

Millwork answered 2/2, 2018 at 20:9 Comment(0)
D
1

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.

 function acf_meta_value_filter() {
  global $typenow;
  global $wp_query;
    if ( $typenow == 'your_custom_post_type_name' ) { // Your custom post type meta_key_name

      $plugins = array( 'value1', 'value2', 'value3' ); // Options for the filter select field
      $current_plugin = '';
      if( isset( $_GET['meta_key_name'] ) ) {   
        $current_plugin = $_GET['meta_key_name']; // Check if option has been selected
      } ?>
      <select name="meta_key_name" id="meta_key_name">
        <option value="all" <?php selected( 'all', $current_plugin ); ?>><?php _e( 'All', '' ); ?></option>
        <?php foreach( $plugins as $key=>$value ) { ?>
          <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $current_plugin ); ?>><?php echo esc_attr( $value ); ?></option>
        <?php } ?>
      </select>
  <?php }
}
add_action( 'restrict_manage_posts', 'acf_meta_value_filter' );

function acf_meta_value_filter_by_slug( $query ) {
  global $pagenow;
  // Get the post type
  $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
  if ( is_admin() && $pagenow=='edit.php' && $post_type == 'your_custom_post_type_name' && isset( $_GET['meta_key_name'] ) && $_GET['meta_key_name'] !='all' && $query->is_main_query()) {
    $query->query_vars['meta_key'] = 'meta_key_name';
    $query->query_vars['meta_value'] = $_GET['meta_key_name'];
    $query->query_vars['meta_compare'] = '=';

  }
}
add_filter( 'parse_query', 'acf_meta_value_filter_by_slug' );
Diabolism answered 16/5, 2019 at 6:15 Comment(0)
T
0

I had a similar problem with a site a took over from other dev.

function anty_search_form( $query ) {
    if ( $query->is_search ) {
        $query->set( 'post_type', array('post', 'product') );
    }
    return $query;
}
add_filter( 'pre_get_posts', 'anty_search_form' );

Someone forgot to exclude this function from wp-admin page. So changing to this helped Invalid post type.

function anty_search_form( $query ) {
    if (!is_admin()){
        if ( $query->is_search ) {
            $query->set( 'post_type', array('post', 'product') );
        }
        return $query;
    }
}
add_filter( 'pre_get_posts', 'anty_search_form' );
Tigress answered 5/1, 2021 at 12:28 Comment(0)
Y
0

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:

add_filter( 'pre_get_posts', 'tgm_io_cpt_search' );
function tgm_io_cpt_search( $query ) {

if ( $query->is_search ) {
$query->set( 'post_type', 'your_custom_post_type' );
}

return $query;
}
Yentai answered 5/1, 2023 at 3:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.