How to search posts by shortcode content in Wordpress?
-
When you ever try to search WordPress posts or pages by shortcode content inserted to posts, you should notice that it's a not trivial task!
Simply say, it's not possible for the native WordPress search. And you even can not solve this by simple PHP code. But why? Well, let me explain to you in detail.
Shortcodes are tags like
[the_shortcode]
included in the post content. Lots of plugins use this technique to insert their custom (and sometimes quite complex) content into the post, and this powerful functionality really makes the difference to us!But why we can't search in that generated content? Because the shortcode content is never stored in the database. Instead, only the
[the_shortcode]
string is stored. WordPress page generation function is creating the text for shortcodes "on the fly". But native WP search is working with the database data!Okay... so it's not possible, right?
No way! Since the WPFTS plugin is indexing, it does provide the ability to store "generated" shortcode content in the search index together with the post content! And thus, posts/pages become searchable by the shortcode content!
Let me give you an example.
Assume we have a post with the content like this:
The cats always have their wonderful and useful devices like [cats_devices].
As you can see, this content has the shortcode, which will be replaced by the string
"fluffy paws, long tail, furry back, and purr"
on the page generation stage. But only the initial content will be stored in the database.Assume you want to find this post by the word "tail" using an integrated WP search. You will find... nothing!
Now listen, what the WPFTS plugin does: at the moment when you save your post, it renders the post content as it should look on the frontend - with all these shortcodes rendered and then store the result in the search index!
Later, when you're trying to search for the word "tail", it will return the post ID, because it knows - the "tail" will be on the page!
And even more - this full phrase will be shown in the Smart Excerpt. Cool, right?
The respective option was added to the
WPFTS Settings / Indexing Engine / Indexing Defaults
tab and it is enabled by default, but who knows, just be sure you have this enabled to use this cool feature!You don't need for Pro version to try this cool functionality: shortcode content search is available in the basic (free) version of WPFTS.
Hope this post made your day. Or an hour. Or at least minute