【问题标题】:Wordpress search three different formsWordpress 搜索三种不同的形式
【发布时间】:2016-08-25 11:08:26
【问题描述】:

我在一个页面中有三种不同的搜索表单。产品、餐厅和页面是不同的帖子类型。尝试通过隐藏输入类型在 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;

        }

请指教。谢谢

【问题讨论】:

  • 如果您发现答案有帮助,请提供您的反馈:)

标签: wordpress search custom-post-type


【解决方案1】:

通过检查您的代码,您似乎想要搜索 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 种帖子类型中搜索您的关键字。

【讨论】:

    【解决方案2】:
    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');
    

    【讨论】:

      猜你喜欢
      • 2016-10-03
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多