php - 在 wordpress 中创建搜索查询

标签 php html wordpress

我正在使用一个名为 Advanced Search 的搜索模块(在此处阅读更多相关信息:http://wpadvancedsearch.com/),基本上我想做的是将我的搜索从一个页面(用户输入查询的页面)推送到接受查询并显示结果的结果页面。下面我将从每个页面发布我的 php。

对于 searchpage.php: 这是 PHP:

<?php
/*
Template Name: Search Page
*/

session_start();

$args = array();
$args['form'] = array(
                    'action' => 'http://adgo.ca/uoftclubs/search-results/');
$args['wp_query'] = array(
                        'post_type' => 'post',
                        'order' => title);
$args['fields'][] = array(
                        'type' => 'meta_key',
                        'format' => 'checkbox',
                        'label' => 'Pick Your Campus:',
                        'compare' => 'IN',
                        'meta_key' => 'campus',
                        'values' => array('utsg' => 'UTSG', 'utsc' => 'UTSC', 'utm' => 'UTM'));
$args['fields'][] = array(
                        'type' => 'search',
                        'placeholder' => 'Search for a club!');
$args['fields'][] = array(
                        'type' => 'submit');

$my_search_object = new WP_Advanced_Search($args);
?>

和相关的html:

<div class="main-search">
        <?php $my_search_object -> the_form(); $_SESSION['searchquery'] = $my_search_object; ?>
    </div>

然后是 search-results.php: php 和 html:

<div id="search-results">
        <?php
        session_start();
        $my_search = $_SESSION['searchquery'];
        $temp_query = $wp_query;
        $wp_query = $my_search->query();
        if ( have_posts() ):
            while ( have_posts() ): the_post(); ?>
                <article>
                    <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                    <?php
                    if ($post->post_type == 'gs_field') the_field('description');
                    else the_excerpt();
                    ?>
                </article>
            <?php
            endwhile;
        else :
            echo '<p>Sorry, no clubs matched what you searched.</p>';
        endif;

        $my_search->pagination();

        $wp_query = $temp_query;
        wp_reset_query();
        ?>
    </div>

我已经设置好了,所以我在 wordpress 中创建了两个页面,每个模板对应一个页面。你可以在第一页看到我正在使用

$args['form'] = array(
                    'action' => 'http://adgo.ca/uoftclubs/search-results/');

将表单提交到我的结果页面。我还使用“$_SESSION”变量将“$my_search_object”传送到下一页。

问题是,当我进行查询并将其重定向到我的结果页面时,查询本身不会继续(即无论我搜索什么,每个帖子都会出现在结果页面上)。当我将结果页面代码与查询页面放在同一页面上时,搜索功能可以完美运行。

毫无疑问,这里犯了一些非常糟糕的错误。我正在学习,所以在您纠正我的错误时我会很感激。

如果您需要任何帮助找出问题所在,请告诉我。

最佳答案

在您的 functions.php 文件中添加以下内容:

function register_session()
{
  if( !session_id() )
  {
    session_start()
  }
}

add_action('init', 'register_session');

从您的页面中删除您的 session_start(); 并从那里开始。这应该让你继续进行 session 。

但是我必须补充一点,这是非常低效的。因为您已经将数据发布到该页面。建议使用搜索程序的内置帖子扩展。

if ( have_posts() ): 
    while ( have_posts() ): the_post();

        the_title();

    endwhile;
endif;

这样您就不会处理数据两次。

关于php - 在 wordpress 中创建搜索查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21282284/

相关文章:

php - 加入返回空列?

html - IE8 中的 webshim 和 checkValidity()

php - 保存帖子元并重复使用它们

php - 如何从此查询中选择最新日期(不在现有表中)?

php - 使用 PHP 将数据从 HTML 文本字段更新到 MySQL 数据库

php - Laravel Carbon 获取尾随数据错误

html - 使用 TestCafe 从 HTML5 日期输入中删除值?

html - 无序列表导航栏元素有奇怪的差距

ios - 导致自托管视频无法在移动设备上运行的幕后原因是什么

mysql - wordpress 主页 http 和 https 都默认设置为 https