Wordpress,如何使用get方法搜索帖子?

Wordpress, how to make search for posts using get method?

所以我正在为自己构建一个 WordPress 主题以满足我的需要,所以我现在正在寻找的是在 php.

中使用 get 方法进行搜索

如果我的 url 是这样的:

http://www.MyWordPressWebsite.com/search.php?string=Bananas+and+apples

在我的 search.php 中,我想做这样的事情:

string = $_GET["string"];

 $fruits_args = array(
    'post_type'     => 'fruits',
    'posts_per_page'    => -1,
    'cat'           =>  'fruits'
); 
 $fruits = new WP_Query($fruits_args);

那么如何使循环与从 get 方法获得的字符串相关?

将搜索字符串与 post 类型、类别和 post 标题进行比较就足够了。

改用 query_posts(),Wordpress 将使用这些参数处理搜索。您可以根据需要添加任意数量的参数。

示例:

<?php $my_post_type = (get_query_var('my_post_type')) ? get_query_var('my_post_type') : false;?>
<?php query_posts(array('post_type' => $my_post_type));?>

<?php //Normal loop here ?>

有关详细信息,请访问 https://codex.wordpress.org/Function_Reference/query_posts

希望对您有所帮助