hosting tutorials WeChat Top

👋🏼Welcome to my WP-Host blog where I am excited to share my knowledge and expertise on WordPress hosting and website construction tutorials with you. Let’s connect and learn from each other! You can reach me at info@yrshare.com.

Email Me

(如果你会中文,可以点击微信图标与我联系。)

扫一扫加我

注:因个人英文水平有限,所以暂时只能为懂中文的朋友提供wordpress建站服务

微信:18200592859 或 yrwordpress

How to Create an Advanced Search Form for a Word Press Custom Post Type?

2023032312182612

How to Create an Advanced Search Form for a Word Press Custom Post Type?This article explains how to create an advanced search form for custom post types in WordPress. The main topics covered include:

  1. The need for an advanced search form and its advantages.
  2. Creating custom fields with the Advanced Custom Fields plugin to collect more useful search information.
  3. Using the Query Monitor plugin to view generated SQL query statements for optimizing search performance.
  4. Writing custom code to handle search form submission and constructing search queries based on user search criteria.
  5. Displaying search results and enabling pagination.
【WP-Host/悦然wordpress建站】

Adding a search form with custom post types to your wordpress site search results can provide a better site experience for your visitors. In this WordPress tutorial, you can learn how to create an advanced search form for a WordPress custom post type.

Why Create a Word Press Custom Post Type Advanced Search Form?

If you have many different content types, a custom post type search form allows you to help your visitors find the most relevant content more easily.

If we add a custom post type search form to each page, then the search results will only show posts of that category.

This custom search helps your visitors view more pages and stay on your site longer, which sends positive WordPress SEO signals like lower bounce rates and increased dwell time.

Create New Search Algorithms Using a Word Press Plugin

The easiest way to create an advanced search form for a custom post type is to use the Search WP plugin (now acquired by Awesome Motive), which is already used by tens of thousands of websites.
It’s easy to use and gives you complete control over your Word Press search results.

The first thing you need to do is install and enable the plugin. Once enabled, you need to go to Settings » Search WP and click on the “License” menu option.

2023032312242954

Then, enter your license key in the “License” box and click the “Activate” button. You can find this information in your account on the Search WP website. Once done, you need to click on the “Engines” menu option.

2023032312244432

This will take you to a screen where you can set your search engine settings. Think of it as your website creating its own Google search algorithm.

With Search WP, you need to create a site-wide search engine before you can create a custom article type search engine, so we’ll do that first. You can adjust your search engine settings by clicking the drop-down menus for Articles, Pages, Library, or other custom article types. Each section has a “Weight Multiplier” that will change how search engines rank the content. If you want search engines to give importance to page titles rather than content, adjust the sliders accordingly.

2023032312253395

When you’re done customizing your default search engine settings, be sure to click the “Save Engines” button to create your first search engine. Now that your initial search engine is set up, it’s time to create an advanced search form for your custom post type.

Add an advanced search form limited to a single custom post type

To create a custom search box, we’ll start by adding a separate search engine that targets just the type of custom article you want. First, navigate to Settings » Search WP and click on the “Add New” button.

2023032312255039

This will automatically create a new standalone search engine. Next, click the “Sources & Settings” button.

2023032312262094

This will bring up a popup where you can choose the source of your custom search engine.
Just uncheck the boxes for the sources you don’t want to include. In this example, we’ll just check the “Coupons” box because we want to create a search form that only allows users to search our coupons page.

You also need to give your search engine a name. Make a note of the name in the [Engine Name] box as you will need it in a later step.

2023032312264249

Then, click the “Done” button. When you’re done, click the “Save Engines” button to save your new custom post type search engine.

Now, you need to download the Search WP Shortcode extension to easily add your new custom article search form to Word Press. First, head to the Search WP Shortcodes Extension website and click on the “Download available with active license” button.

2023032312265840

Afterwards, you need to install and enable the extension the same way you installed the plugin above.
You can now create new custom article search form pages using shortcodes.

Just navigate to Pages » New Page and give your page a name. After that, click on the plus “Add Block” icon to bring up the block menu.

2023032312271426

Next, type “HTML” into the search box. Then, select the “Custom HTML” block.

2023032312273093

Once done, you can add the following shortcodes and HTML to your page.

[searchwp_search_form engine="coupon" var="searchvar" button_text="Find Results"]
  
<div class="search-results-wrapper">
 [searchwp_search_results engine="coupon" var="searchvar" posts_per_page=4]
  <h2>[searchwp_search_result_link direct="true"]</h2>
  [searchwp_search_result_excerpt]
 [/searchwp_search_results]
</div>
  
<div class="no-search-results-found">
 [searchwp_search_results_none]
 No results found, please search again.
 [/searchwp_search_results_none]
</div>
  
<div class="search-results-pagination">
 [searchwp_search_results_pagination direction="prev" link_text="Previous" var="searchvar" engine="my_test_engine"]
 [searchwp_search_results_pagination direction="next" link_text="Next" var="searchvar" engine="my_test_engine"]
</div>

The code above will add your search engine, create a section to display the search results, display a no result message if necessary, and add pagination if the results go to multiple pages.

You need to change the engine name from [engine=”coupon”] to your own engine name. The rest of the code can remain unchanged.

2023032312275577

Make sure you click Publish or Update to save your page and make it live. Your custom article type search engine is now live on your website.

2023032312281539

Search WP is a very flexible and customizable search plugin. You can also use this plugin to add live Ajax search, create search by category functionality, and more. We hope this tutorial helped you learn how to create a Word Press custom post type advanced search form to further enhance your website search experience.

在wordpress网站搜索结果中添加包含自定义文章类型的搜索表单,可以为访问者提供更好的网站体验。在这个wordpress教程中,你可以学到如何为WordPress自定义文章类型创建高级搜索表单。

为什么创建WordPress自定义文章类型高级搜索表单?

如果您有许多不同的内容类型,那么自定义文章类型搜索表单可让您帮助访问者更轻松地找到最相关的内容。

如果我们向每个页面添加自定义文章类型搜索表单,那么搜索结果将仅显示该类别的帖子。

这种自定义搜索可帮助您的访问者查看更多页面并在您的网站上停留更长时间,这会发出积极的WordPress SEO信号,例如较低的跳出率和增加的停留时间。

使用WordPress插件创建新的搜索算法

为自定义文章类型创建高级搜索表单的最简单方法是使用SearchWP插件(目前该插件已经被Awesome Motive收购),目前已经有几万个网站使用这款WordPress搜索插件。它易于使用,并且让您可以完全控制您的WordPress搜索结果。

您需要做的第一件事是安装并启用插件。启用后,您需要转到设置»SearchWP,然后单击“License”菜单选项。

2023032312242954

然后,在“License”框中输入您的许可证密钥,然后单击“Activate”按钮。您可以在SearchWP网站上的帐户中找到此信息。完成后,您需要单击“Engines”菜单选项。

2023032312244432

这会将您带到一个屏幕,您可以在其中设置搜索引擎设置。可以把它想象成为您的网站创建自己的Google搜索算法。

使用SearchWP,您需要先创建一个站点范围的搜索引擎,然后才能创建自定义文章类型搜索引擎,因此我们将首先执行此操作。您可以通过单击“文章”、“页面”、“图书馆”或其他自定义文章类型下拉菜单来调整搜索引擎设置。每个部分都有一个“权重乘数(Weight Multiplier)”,它将改变搜索引擎对内容的排名方式。如果您希望搜索引擎重视页面标题而不是内容,请相应地调整滑块。

2023032312253395

完成自定义默认搜索引擎设置后,请务必单击“Save Engines”按钮以创建您的第一个搜索引擎。现在您的初始搜索引擎已设置,是时候为您的自定义文章类型创建高级搜索表单了。

添加仅限于单个自定义文章类型的高级搜索表单

要创建自定义搜索框,我们将首先添加一个单独的搜索引擎,该引擎仅针对您想要的自定义文章类型。首先,导航到设置»SearchWP,然后单击“Add New”按钮。

2023032312255039

这将自动创建一个新的独立搜索引擎。接下来,单击“Sources & Settings”按钮。

2023032312262094

这将弹出一个弹出窗口,您可以在其中选择自定义搜索引擎的来源。只需取消选中您不想包含的来源的框即可。在此示例中,我们将只选中“Coupons”框,因为我们希望创建一个仅允许用户搜索我们的优惠券页面的搜索表单。

您还需要为您的搜索引擎命名。记下“Engine Name”框中的名称,因为您将在稍后的步骤中用到它。

2023032312264249

然后,单击“Done”按钮。完成后,单击“Save Engines”按钮以保存新的自定义文章类型搜索引擎。

现在,您需要下载SearchWP简码扩展,以便轻松地将新的自定义文章搜索表单添加到WordPress。首先,前往SearchWP Shortcodes Extension网站,然后单击“Download available with active license”按钮。

2023032312265840

之后,您需要按照安装上述插件的相同方式安装和启用扩展。现在,您可以使用短代码创建新的自定义文章搜索表单页面。

只需导航到页面»新建页面,然后为您的页面命名。之后,单击加号“添加区块”图标以显示块菜单。

2023032312271426

接下来,在搜索框中输入“HTML”。然后,选择“自定义HTML”区块。

2023032312273093

完成后,您可以将以下短代码和HTML添加到您的页面。

[searchwp_search_form engine="coupon" var="searchvar" button_text="Find Results"]
  
<div class="search-results-wrapper">
 [searchwp_search_results engine="coupon" var="searchvar" posts_per_page=4]
  <h2>[searchwp_search_result_link direct="true"]</h2>
  [searchwp_search_result_excerpt]
 [/searchwp_search_results]
</div>
  
<div class="no-search-results-found">
 [searchwp_search_results_none]
 No results found, please search again.
 [/searchwp_search_results_none]
</div>
  
<div class="search-results-pagination">
 [searchwp_search_results_pagination direction="prev" link_text="Previous" var="searchvar" engine="my_test_engine"]
 [searchwp_search_results_pagination direction="next" link_text="Next" var="searchvar" engine="my_test_engine"]
</div>

上面的代码将添加您的搜索引擎,创建一个部分来显示搜索结果,必要时显示无结果消息,如果结果进入多个页面,则添加分页。

您需要将引擎名称更改engine="coupon"为您自己的引擎名称。其余代码可以保持不变。

2023032312275577

确保您单击“发布”或“更新”以保存您的页面并使其生效。您的自定义文章类型搜索引擎现已在您的网站上发布。

2023032312281539

SearchWP是一个非常灵活和可定制的搜索插件。您还可以使用此插件添加实时Ajax搜索、创建按类别搜索功能等。我们希望本教程能帮助您了解如何创建WordPress自定义文章类型高级搜索表单,进一步提升网站搜索体验。

WordPress Hosting / 悦然wordpress建站

WordPress Hosting / 悦然wordpress建站

【WordPress Hosting / 悦然wordpress建站】I am a WordPress hobby from China, and I like to communicate with friends from all over the world to learn WordPress website building and maintenance related knowledge.