【问题标题】:Creating a search query in wordpress在 wordpress 中创建搜索查询
【发布时间】:2014-03-07 17:31:47
【问题描述】:

我正在使用一个名为 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”带到下一页。

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

毫无疑问,这里犯了一些非常糟糕的错误。我正在学习,所以我很高兴在你纠正我的错误时得到教导。

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

【问题讨论】:

标签: php html wordpress


【解决方案1】:

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

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

add_action('init', 'register_session');

从您的页面中删除您的session_start();,然后从那里开始。这应该让你继续进行会话。

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

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

        the_title();

    endwhile;
endif;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 2017-06-30
    • 1970-01-01
    • 2014-10-30
    • 2020-02-24
    • 2021-07-23
    • 2011-03-07
    相关资源
    最近更新 更多