
wordpress 自定义
Have you ever worked on a WordPress site that has a lot of custom post types? Well as-is WordPress search is a disaster which is why many bloggers use Google custom search. Well, we were working on a site that has a good amount of resource articles, videos, books, and blog posts. Using the default WordPress search just wasn’t cutting it. Using Google Search also was not a viable option. Therefore, we decided to create an advanced search form which lets user pick which area of the site they want to search by limiting custom post types via checkboxes. Users can combine their search queries and most importantly, we made it so the checkboxes are saved along with the search query. This lets the user sees exactly what they searched for, and they can modify the parameters. In this article, we will show you how to create an advanced search form in WordPress utilizing the power of the WordPress search query and limiting the results by custom post types.
您是否曾经在具有很多自定义帖子类型的WordPress网站上工作? WordPress搜索本身就是一场灾难,这就是为什么许多博客使用Google自定义搜索的原因 。 好吧,我们正在一个拥有大量资源文章,视频,书籍和博客文章的网站上工作。 使用默认的WordPress搜索并没有减少它。 使用Google搜索也不是一个可行的选择。 因此,我们决定创建一个高级搜索表单,允许用户通过复选框限制自定义帖子类型,从而选择他们要搜索的站点区域。 用户可以结合他们的搜索查询,最重要的是,我们做到了,因此复选框与搜索查询一起保存。 这使用户可以准确地看到他们搜索的内容,并且可以修改参数。 在本文中,我们将向您展示如何利用WordPress搜索查询的功能在WordPress中创建高级搜索表单,并通过自定义帖子类型限制结果。
First thing you need to do is open your searchform.php file or wherever your search form is stored. Then add the following fields inside the form code:
您需要做的第一件事是打开searchform.php文件或存储搜索表单的任何位置。 然后在表单代码内添加以下字段:
<input type="hidden" name="post_type[]" value="articles" />
<input type="hidden" name="post_type[]" value="post" />
<input type="hidden" name="post_type[]" value="videos" />
<input type="hidden" name="post_type[]" value="books" />
Don’t forget to replace the value with your custom post types. The code above basically limits your search results to those post types. Well, if you noticed we pretty much added all post types available except for pages. Well, there is a good reason for doing so which we will get to later. So make sure to include ALL post types that you want to search for using the main search button. These fields are hidden, so the user doesn’t see these.
不要忘记用自定义帖子类型替换该值。 上面的代码基本上将您的搜索结果限制为这些帖子类型。 好吧,如果您注意到我们几乎添加了除页面之外的所有可用帖子类型。 好吧,这样做有一个很好的理由,我们稍后会讲到。 因此,请确保包含要使用主搜索按钮搜索的所有帖子类型。 这些字段是隐藏的,因此用户看不到它们。
Next open your search.php file and paste the following codes above your loop content, so your users can see the options at the top.
接下来打开您的search.php文件,并将以下代码粘贴到循环内容上方,以便您的用户可以在顶部看到这些选项。
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" <?php if(is_search()) { ?>value="<?php the_search_query(); ?>" <?php } else { ?>value="Enter keywords …" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"<?php } ?> /><br />
<?php $query_types = get_query_var('post_type'); ?>
<input type="checkbox" name="post_type[]" value="articles" <?php if (in_array('articles', $query_types)) { echo 'checked="checked"'; } ?> /><label>Articles</label>
<input type="checkbox" name="post_type[]" value="post" <?php if (in_array('post', $query_types)) { echo 'checked="checked"'; } ?> /><label>Blog</label>
<input type="checkbox" name="post_type[]" value="books" <?php if (in_array('books', $query_types)) { echo 'checked="checked"'; } ?> /><label>Books</label>
<input type="checkbox" name="post_type[]" value="videos" <?php if (in_array('videos', $query_types)) { echo 'checked="checked"'; } ?> /><label>Videos</label>
<input type="submit" id="searchsubmit" value="Search" />
</form>
This will add a search box above your results with the search query inside the input box. This will also check which post types are being searched for in the query, and make the appropriate checkboxes checked. Remember, how we added all post types in the hidden field. Well, we added it just so we can run the in_array check and keep the checkboxes checked. There was no documentation on how to do this otherwise, so this was the best way we found that does the job. Below is a preview of how the search box looks:
这将在结果上方添加一个搜索框,并在输入框内添加搜索查询。 这还将检查在查询中搜索哪些帖子类型,并选中相应的复选框。 记住,我们如何在隐藏字段中添加所有帖子类型。 好吧,我们添加了它只是为了可以运行in_array检查并使复选框保持选中状态。 没有关于如何执行此操作的文档,因此这是我们发现完成此操作的最佳方法。 以下是搜索框外观的预览:

From there, the user can simply modify the parameter as they please.
从那里,用户可以根据需要简单地修改参数。
Hopefully this article helped those in need. When we are doing the research, there were bunch of incomplete articles. Found the answer of adding the checkboxes in the WordPress support forum in an older thread however they were only talking about hidden fields. Whereas we wanted to give users the option to modify the search query. Props to @tammyhart for pointing us in the right way to do the checked query for the variable.
希望本文可以帮助那些有需要的人。 当我们进行研究时,有很多不完整的文章。 在较旧的主题中找到了在WordPress支持论坛中添加复选框的答案,但是他们只是在谈论隐藏的字段。 而我们希望为用户提供修改搜索查询的选项。 支持@tammyhart,以正确的方式为我们指出已检查的变量查询。
wordpress 自定义