WordPress search customization is a topic we at Seravo get asked about on a frequent basis. There are many different ways to customize the search, and customers understandably want to learn the best practices. The search can be customized quite easily with small changes on PHP code level, and by utilizing MariaDB database’s built-in search functionality. You can also choose a more robust way to do this, and build a new ElasticSearch server just for your case.
These slides are from the webinar on January 14th, 2021: https://seravo.com/blog/webinar-search-function-and-how-to-customize-it/
5. A comparison published at
ismyhostfastyet.com by Google
employees rank Seravo as the
world’s fastest WordPress hosting
service.
The study used time-to-first-byte
(TTFB) measurement data from real
users as collected from the Chrome
browser UX data.
6. Every content type (post, pages, media, comments etc) view
has a search box in the top right corner.
The number of results on page can be configured via Screen
Options > Number of items per page.
7. In any post or page, add Gutenberg block
“Search”. There are also a couple of options
to control how the search box looks like.
In Appearance > Widgets configure a
“Search” to appear in the footer or sidebar,
depending on what the selected theme
supports.
Many themes have also rolled their own
search settings.
8. Even if a WordPress site does not have a
search box available, one can always
manually enter ?s=keyword to run the
search.
Even though the s argument is quite
universal, Google Analytics does not support
it out-of-the-box and you need to configure it
in your Google Analytics view settings to get
search statistics.
12. Let’s find out
1. Install Debug Bar
2. Define in wp-config:
3. Search (?s=demo)
4. View Debug > Queries
SELECT wp_posts.ID FROM wp_posts WHERE 1=1
AND (((wp_posts.post_title LIKE '%demo%') OR
(wp_posts.post_excerpt LIKE '%demo%') OR
(wp_posts.post_content LIKE '%demo%'))) AND
wp_posts.post_type IN ('post', 'page', 'attachment')
AND (wp_posts.post_status = 'publish' OR
wp_posts.post_author = 1 AND
wp_posts.post_status = 'private') ORDER BY
wp_posts.post_title LIKE '%demo%' DESC,
wp_posts.post_date DESC LIMIT 0, 10
= Title and contents of published, non-private
post, page and attachment title or contents.
13. Register custom post types with correct
settings, and remember to validate them by
proper testing!
developer.wordpress.org/plugins/post-types/
registering-custom-post-types/
developer.wordpress.org/reference/function
s/register_post_type/#exclude_from_search
(boolean) Whether to exclude posts with this
post type from front end search results.
Default: value of the opposite of public
argument
● ‘true’ – site/?s=search-term will not
include posts of this post type.
● ‘false’ – site/?s=search-term will
include posts of this post type.
14. If you have custom post types, which
typically also have custom post meta fields
and you want the search to look into them as
well – it will become pretty complicated (and
slow!).
It can however be done by hooking into
filters:
● posts_join
● posts_where
● posts_distinct
Sign up for our developer newsletter!
You’ll get notified when we publish the code
how to include meta fields in the WordPress
search in an upcoming blog post.
15.
16.
17. Due to heavy use of LIKE ‘%term%’ that
causes full table scans instead of using
database indexes.
SELECT wp_posts.ID FROM wp_posts WHERE 1=1
AND (((wp_posts.post_title LIKE '%demo%') OR
(wp_posts.post_excerpt LIKE '%demo%') OR
(wp_posts.post_content LIKE '%demo%'))) AND
wp_posts.post_type IN ('post', 'page', 'attachment')
AND (wp_posts.post_status = 'publish' OR
wp_posts.post_author = 1 AND
wp_posts.post_status = 'private') ORDER BY
wp_posts.post_title LIKE '%demo%' DESC,
wp_posts.post_date DESC LIMIT 0, 10
18. If the search included two terms seravo and demo:
SELECT wp_posts.ID FROM wp_posts WHERE 1=1 AND (((wp_posts.post_title LIKE '%seravo%')
OR (wp_posts.post_excerpt LIKE '%seravo%') OR (wp_posts.post_content LIKE '%seravo%'))
AND ((wp_posts.post_title LIKE '%demo%') OR (wp_posts.post_excerpt LIKE '%demo%') OR
(wp_posts.post_content LIKE '%demo%'))) AND wp_posts.post_type IN ('post', 'page',
'attachment') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND
wp_posts.post_status = 'private') ORDER BY (CASE WHEN wp_posts.post_title LIKE '%seravo
demo%' THEN 1 WHEN wp_posts.post_title LIKE '%seravo%' AND wp_posts.post_title LIKE
'%demo%' THEN 2 WHEN wp_posts.post_title LIKE '%seravo%' OR wp_posts.post_title LIKE
'%demo%' THEN 3 WHEN wp_posts.post_excerpt LIKE '%seravo demo%' THEN 4 WHEN
wp_posts.post_content LIKE '%seravo demo%' THEN 5 ELSE 6 END), wp_posts.post_date DESC
LIMIT 0, 10;
The database server needs to be insanely optimized to handle these in milliseconds!
Luckily our MariaDB servers at Seravo are top notch :)
20. Easy way:
1. Install Relevanssi Light
2. Activate
For a customer with 4 GB wp_posts table
search result page load time dropped from
20 seconds to 0,2 seconds when MariaDB
FULLTEXT search was applied, and also the
relevance improved = first results were most
useful for visitors.
Hard way:
1. Learn Finnish
2. Read
wp-palvelu.fi/blogi/wordpressin-haku-
nopeaksi-mariadblla/
3. Create index via MariaDB console
4. Write a filter for posts_search in your
custom theme/plugin
21. Read more at: seravo.com/docs/get-started/available-commands/
22.
23. Store the result of a heavy/slow computation to avoid having to do it all
the time (e.g. HTTP requests or heavy database queries):
More at: seravo.com/blog/faster-wordpress-with-transients/
and developer.wordpress.org/apis/handbook/transients/
24.
25. External services can cost $$$ and €€€, in
particular for big sites that are the ones
experiencing the challenges. A custom
Elastic Search instance is easily more
expensive than a high-tier WordPress
hosting plan.
For external search to work, you need to
constantly submit all content to external
server, which adds delays and eats up server
traffic and CPU capacity.
The best solution is to have the search
in-place, in WordPress and where the data is.
Examples:
● Jetpack
● ElasticPress
● Elastic Search self-hosted or SaaS
● Algolia
● AddSearch
● Google
○ Custom search page: cse.google.com
○ Good and cheap, but has ads
31. Actually the default WordPress search looks
like this in SQL:
…
Followed by:
Why not use us COUNT(*) which is faster?
WordPress legacy reasons, just like lack of
native gettext() and why still latching on to
SVN instead of git..
Skip the use of SQL_CALC_FOUND_ROWS if you
don’t need to know the result size and don’t use
pagination:
32. The database is often the
performance bottleneck. See my
other talk on WordPress and
database optimization:
More in-depth code profiling tips
in my WordPress performance
talk:
wordpress.tv/?s=otto+kekäläinen
33. Make Your Site Faster with Caching
https://seravo.com/blog/wordpress-cache/
300% faster WordPress load times with transients
https://seravo.com/blog/faster-wordpress-with-transients/
5 common reasons why your WordPress site is slow
https://youtu.be/8sJExUO-U4A
Improving WordPress Performance with XDebug and PHP Profiling
https://youtu.be/oKcIS5A-6_c