2017-03-02 105 views
0

我有一個配方網站和搜索選項。現在我有食譜名稱CAKE,當我搜索CAKES然後它沒有顯示結果。它可以用CAKECAK。我想要LIKE查詢的結果。請幫助我。WordPress搜索不能正常工作

這裏是我的自定義搜索ARGS代碼:

<?php $args = array('posts_per_page' => -1, 'post_type' => 'recipes', 's' => $_GET['s']); 
$myposts = get_posts($args); 
if (empty($myposts)) { 
    echo 'No results found in Recipe!'; 
} else { ?> 
//show recipes 
    <?php wp_reset_postdata(); 
} ?> 
+0

哪個功能用。 –

+0

如果配方是一個類別只是使用類別分類和通過蛋糕匹配,這將幫助您獲得結果。你所做的會嘗試匹配發布標題和搜索關鍵字,你認爲這應該給你的職位? –

+0

我正在使用get_posts()函數 –

回答

0

試試這個

$args = array('posts_per_page' => -1, 'post_type' => 'recipes','s' => $_GET['s']); 

$the_query = new WP_Query($args); 

// The Loop 
if ($the_query->have_posts()) { 

    while ($the_query->have_posts()) { 
     $the_query->the_post(); 
     //whatever you want to do with each post 
    } 
} else { 
    // no posts found 
} 
+0

沒有與我的代碼相同。 –

+0

安裝此插件https://wordpress.org/plugins/relevanssi/ –

0

這麼多年的研究,我發現一個備用。我用一些PHP操作將複數形式改爲單數形式。這是我分享它。

$str = $_GET['s']; 
$result = rtrim($str, 's'); 
$result_sing = str_pad($result, strlen($str) - 1, 's'); 
$args = array( 
'posts_per_page' => -1, 
'post_type' => 'recipes', 
's' => $result_sing, 
);