【问题标题】:Limiting Wordpress Search to specific blog category not working将 Wordpress 搜索限制为特定的博客类别不起作用
【发布时间】:2022-01-12 21:59:21
【问题描述】:

所以我试图在博客页面上创建一个搜索表单,以便它只搜索类别为“博客”的帖子 这是我正在使用的搜索表单代码:

<form  method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">

                   <input type="hidden"  name="cat" id="blog" value="7" />
                   
                    <input type="text" value="<?php echo get_search_query(); ?>" name="s" />
                    <input type="submit" value="Search" />
            </form> 

这是 search.php 代码:

<?php
/**
 * The template for displaying search results pages
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
 *
 * @package Scrap
 */

get_header();
?>

    <main id="primary" class="site-main">

        <?php
$s=get_search_query();
$args = array(
                's' =>$s
            );
    // The Query
$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {
        _e("<h2 style='font-weight:bold;color:#000'>Search Results for: ".get_query_var('s')."</h2>");
        while ( $the_query->have_posts() ) {
           $the_query->the_post();
                 ?>
                    <li>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </li>
                 <?php
        }
    }else{
?>
        <h2 style='font-weight:bold;color:#000'>Nothing Found</h2>
        <div class="alert alert-info">
          <p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p>
        </div>
<?php } ?>

    </main><!-- #main -->

<?php
// get_sidebar();
get_footer();

搜索查询时,URL 加载就像它被限制在类别中一样:https://www.scraperapi.com/?cat=7&s=proxies 但它仍然显示来自该类别之外的页面,并且它不会加载与搜索查询匹配的所有博客文章。 搜索表单位于 https://www.scraperapi.com/blog/,但它显示:隐藏在顶部名为“blog-search”的 div 中。

非常感谢您提供的任何帮助!

【问题讨论】:

    标签: php wordpress search search-form


    【解决方案1】:

    尝试使用get_query_var 获取 query_variable “cat”,并将 cat 变量也包含在 WP_Query 中。还有一些关于此的好信息here。目前$args 只包含带有变量$s 的搜索词。

    这是改编的$args

    $cat = get_query_var('cat');
    $s=get_search_query();
    $args = array(
                's' => $s,
                'cat' => $cat
            );
    //etc..
    

    或者,也可以修改 $wp_query 全局变量。这可以通过设置 $wp_query 的 tax_query 来完成。

    global $wp_query;
    $tax_query[] = array(
    array(
        'taxonomy' => 'cat',
        'field'    => 'id',
        'terms'    => $cat,
      )
    );
    $wp_query->set( 'tax_query', $tax_query );
    

    【讨论】:

    • 天哪,非常感谢!我使用了第一个修复程序,它就像一个魅力。
    【解决方案2】:

    您可以通过pre_get_posts钩子过滤器拦截和修改查询。

    <?php
    
    //function.php
    add_filter( 'pre_get_posts', 'wpso_70266736' );
    if ( ! function_exists( 'wpso_70266736' ) ) {
        function wpso_70266736( $query ) {
            if( $query->is_search() && $query->is_main_query() && !isset( $_GET['category'] ) ) {
                $query->set_404();
                status_header( 404 );
                get_template_part( 404 );
                exit();
            };
        };
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多