2014-01-22 74 views
2

我正在使用名爲高級搜索的搜索模塊(在此處閱讀更多內容:http://wpadvancedsearch.com/),基本上我想要做的就是將我的搜索從一個頁面 - 用戶輸入他們的頁面查詢 - 查詢結果頁面並顯示結果。下面我會從每個頁面發佈我的PHP。在wordpress中創建搜索查詢

對於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> 

,然後在搜索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中創建了兩個頁面,一個f或這些模板中的每一個。你可以在第一頁看到我正在使用

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

將表單提交到我的結果頁面。我還使用「$ _SESSION」變量將「$ my_search_object」帶到下一頁。

問題是,當我進行查詢並重定向到我的結果頁面時,查詢本身並沒有結束(即無論我搜索每一篇文章都會出現​​在結果頁面上)。當結果頁面代碼與查詢頁面位於同一頁面時,搜索功能完美地工作。

毫無疑問,在我的腦海裏,這裏有一些非常差的錯誤。我正在學習,所以我希望在你糾正我的錯誤時接受教育。

讓我知道你是否需要任何幫助你找出問題的地方。

+0

你在使用什麼會議?以下是有關如何設置表單,字段及其操作的示例。 http://wpadvancedsearch.com/docs/form-arguments/ –

回答

0

在你的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; 

這樣你就不會兩次處理數據。