You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress后台二次搜索触发Invalid Post Type错误求助

WordPress后台二次搜索抛出「Invalid Post Type」错误解决方案

问题概述

后台(如文章模块)首次搜索正常,二次搜索已搜索过的内容时,抛出「Invalid Post Type」错误。已排除插件影响,问题出自自定义搜索代码。

相关代码

Functions.php

// Number of search results displayed
function change_wp_search_size($query) {
  if ( $query->is_search ) // Make sure it is a search page
      $query->query_vars['posts_per_page'] = 6; // Change 10 to the number of posts you would like to show

  return $query; // Return our modified query variables
}
add_filter('pre_get_posts', 'change_wp_search_size'); // Hook our custom function onto the request filter

搜索结果模板(includes/section-searchresults.php)

<?php if( have_posts() ): while( have_posts() ): the_post();?>

    <div class="individual-search">

    <?php if(has_post_thumbnail()):?>
        <a href="<?php the_permalink();?>" class="readmore">
            <img src="<?php the_post_thumbnail_url('blog-small');?>" alt="<?php the_title();?>" class="img-fluid mb-3 img-thumbnail mr-4">

            <!-- <?php the_post_thumbnail(  'blog-small', [ 'class' => 'img-fluid mb-3 img-thumbnail', 'alt' => '<?php the_title();?>'] ); ?> -->

        <?php endif;?>

        <div class="blog-content search-results">

            <h3><?php the_title(); ?></h3>
            <?php the_excerpt();?>

            <span class="readmore-txt"><?php echo __('Read more', 'themedomain'); ?></span></a>

        </div>

    </div>

<?php endwhile; else: ?> 

 
<?php echo __('There is no results on the search', 'themedomain'); ?> <?php echo get_search_query();?>

    
<!--     There is no results on the search: '<?php echo get_search_query();?>' -->
    
<?php endif;?>

Search.php

<?php get_header(); ?>

<section class="page-wrap">
    <div class="container">

        <div class="search-wrapper">

<!--         <h1>Search results for: <?php echo get_search_query();?></h1> -->

        <h1><?php echo __('Search results for', 'themedomain'); ?> <?php echo get_search_query();?></h1>

        <section class="search-flex">
        <?php get_template_part('includes/section', 'searchresults'); ?> 

</section>

<div class="pagination">
        <?php
        global $wp_query;

        $big = 9999999999; // Need an unlikely integer

        echo paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $wp_query->max_num_pages
        ) );
        ?>
         <!-- Pagination -->
         
</div>

    </div>
</section>

<?php get_footer(); ?> 

搜索表单代码

<form action="<?php echo home_url('/'); ?>" method="get">
    <input type="hidden">
    <input placeholder="<?php echo __('Search...', 'themedomain');?>" type="text" name="s" id="search" value="<?php the_search_query();?>" required>
    <button type="submit"><?php echo __('Search', 'themedomain');?></button>
</form>

问题根源分析

问题出在pre_get_posts过滤器的逻辑上:

  • pre_get_posts会作用于所有WP_Query实例,包括后台的搜索查询
  • 原代码仅判断$query->is_search,没有排除后台环境,导致后台的搜索查询也被修改
  • 后台搜索时会自动传递post_type参数(比如文章模块的post),二次搜索时,被篡改的查询参数会引发「Invalid Post Type」错误

另外,搜索表单中的空<input type="hidden">可能传递无效参数,也会加重参数冲突问题。

解决方案

1. 修正pre_get_posts过滤器逻辑

修改Functions.php中的函数,添加后台排除和主查询判断,确保只修改前台的主搜索查询:

// 修改搜索结果数量
function change_wp_search_size($query) {
  // 仅作用于前台的主搜索查询
  if ( !is_admin() && $query->is_main_query() && $query->is_search ) {
      $query->query_vars['posts_per_page'] = 6;
  }
  return $query;
}
add_filter('pre_get_posts', 'change_wp_search_size');

2. 清理搜索表单中的无效隐藏输入

删除搜索表单中的空<input type="hidden">,避免传递无效参数:

<form action="<?php echo home_url('/'); ?>" method="get">
    <input placeholder="<?php echo __('Search...', 'themedomain');?>" type="text" name="s" id="search" value="<?php the_search_query();?>" required>
    <button type="submit"><?php echo __('Search', 'themedomain');?></button>
</form>

原理说明

  • !is_admin():确保代码只在前台执行,不干扰后台查询
  • $query->is_main_query():仅修改WordPress的主查询,避免影响其他自定义查询
  • 移除空隐藏输入:防止无效参数干扰搜索请求的参数解析

内容的提问来源于stack exchange,提问作者Dannie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 23:47:36