Wordpress搜索三种不同的形式



我在一个页面中有三种不同的搜索形式。产品、餐厅和页面是不同的帖子类型。尝试通过隐藏输入类型在search.php中显示结果。结果并不像我预料的那样。我的代码有问题。请提供意见或建议。

形式1

<form role="search" method="get"  action="<?php bloginfo('url'); ?>">
<input type="text" value="" name="s" id="s" />
<input type="hidden" value="Restaurant" name="post_type" />
<input type="submit" id="searchsubmit" value="Search" />
</form>

形式2

<form role="search" method="get" action="<?php bloginfo('url'); ?>">
<input type="text" value="" name="s" id="s" />
<input type="hidden" value="Products" name="post_type" />
<input type="submit" id="searchsubmit" value="Search" />
</form>

形式3

<form role="search" method="get" action="<?php bloginfo('url'); ?>">
                        <input type="text" name="search" placeholder="Search...">
                        <input type="hidden" name="post_type" value="Pages" />
                    </form>

以下是我的search.php结构

$myvalue = $_GET['post_type'];
        if ($myvalue == "Restaurant"){
            echo "res";
            function SearchFilter($query) {
                if ($query->is_search) {
                    $query->set('post_type', 'sanha-restau');
                }
                return $query;
            }
            add_filter('pre_get_posts','SearchFilter');
        if ( have_posts() ) :
            while ( have_posts() ) : the_post();
                echo '<ul><li><strong>';
                echo get_the_title()  . '</strong><br> (';
                echo substr(get_the_excerpt(), 0, 200) . ') ';
                echo '</li></ul>';              
            endwhile;
            else :
                get_template_part( 'content', 'none' );
        endif;

        } else if ($myvalue == "Products"){
            echo "pro";
            function SearchFilter($query) {
                if ($query->is_search) {
                    $query->set('post_type', 'sanha-product');
                }
                return $query;
            }
            add_filter('pre_get_posts','SearchFilter');
        if ( have_posts() ) :
            while ( have_posts() ) : the_post();
                echo '<ul><li><strong>';
                echo get_the_title()  . '</strong><br> (';
                echo substr(get_the_excerpt(), 0, 200) . ') ';
                echo '</li></ul>';              
            endwhile;
            else :
                get_template_part( 'content', 'none' );
        endif;

        } else if ($myvalue == "Pages"){
            echo "pages";
            if ( have_posts() ) :
                while ( have_posts() ) : the_post();
                    echo '<ul><li><strong>';
                    echo get_the_title()  . '</strong><br> (';
                    echo substr(get_the_excerpt(), 0, 200) . ') ';
                    echo '</li></ul>';              
                endwhile;
                else :
                    get_template_part( 'content', 'none' );
            endif;
        }

请给我建议。感谢

通过检查代码,您似乎希望在3种帖子类型中进行搜索

Products, Restaurants and Pages

根据文档,您可以使用post_type的数组在多个帖子类型中进行搜索。

其中WP_QUERY中的s参数

Show posts based on a keyword search.
s (string) - Search keyword.
Show Posts based on a keyword search
Display posts that match the search term "abc":

注意:我已经使用abc进行搜索您可以将其更改为esc_sql($_REQUEST['s'])

$searchQuery = new WP_Query(array(
    'post_type' => array('products', 'restaurants','page'),
    's' => 'abc' // <<-- change this to make it work --> esc_sql($_REQUEST['s'])
));
if ($searchQuery->have_posts() ) :
    while ( $searchQuery->have_posts() ) : $searchQuery->the_post();
        echo '<ul><li><strong>';
        echo get_the_title()  . '</strong><br> (';
        echo substr(get_the_excerpt(), 0, 200) . ') ';
        echo '</li></ul>';              
    endwhile;
    else :
        get_template_part( 'content', 'none' );
endif;

因此将有一个查询用于搜索,而不需要使用add_filter。以上WP_Query将在3种帖子类型中搜索您的关键词。

function load_custom_search_template(){
    if(isset($_REQUEST['custom_search'])){
        require('search.php');
        die();
    }
}
add_action('init','load_custom_search_template');

该代码适用于所有

和下面的search.php 代码

$myquery = '';
function searchfilter($query) {
    if ($query->is_search && !is_admin() ) {
        if(isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
                if($type == 'sanha-product') {
                    $query->set('post_type',array('sanha_product'));
                    $myquery = 'Products Found For : ';
                }
                elseif($type == 'sanha-restau'){
                    $query->set('post_type',array('sanha_restau'));
                    $myquery = 'Resaurants Found For : ';
                }
                elseif($type == 'pages'){
                    $query->set('post_type',array('pages'));
                    $myquery = 'Search Result For : ';
                }
        }       
    }
return $query;
}
add_filter('pre_get_posts','searchfilter');

最新更新