WordPress搜索无法正常工作

时间:2017-03-02 06:42:01

标签: wordpress search

我有一个食谱网站和搜索选项。现在我有食谱名称 CAKE ,当我搜索 CAKES 时,它没有显示结果。它很好用 CAKE CAK 。我想要LIKE查询的结果。请帮帮我。

这是我的自定义搜索参数代码:

<?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();
} ?>

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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,
);