Do you want to allow users to filter posts and pages on your WordPress site? Recently, one of our users asked us how to add filters to their WordPress site allowing users to filter posts by category, tags, post types, etc. In this article, we will show you how to let users filter posts and pages in WordPress.
Why Let Users Filter Posts and Pages in WordPress?
By default, WordPress comes with categories and tags as a way for you to sort content into topics and for your users to easily find it.
However, users can only view one category, tag, or archive page at a time and it will only show one post type.
What if you wanted users to be able to filter content in more than one category or tag? How about allowing users to view posts, pages, and custom post types at the same time?
This kind of filtering can be particularly helpful when you have a lot of content, and you want users to filter it according to their own preferences.
Having said that, let’s take a look at how to easily let users filter posts and pages in WordPress.
Allowing Users to Easily Filter Posts and Pages
Upon activation, the plugin will add a new menu item labeled Search & Filter to your WordPress admin bar. Clicking on it will take you to the plugin’s usage page with detailed documentation on how to use the plugin.
Search & Filter plugin comes with a shortcode which accepts different parameters to display the filtering options. You can use this shortcode in a post, page, or inside a text widget.
You can also use the shortcode as a template tag in your WordPress theme files like this:
<?php echo do_shortcode('[searchandfilter fields="search,category,post_tag"]'); ?>
This will display the filtering options with search, category, and tags fields.
By default the plugin accepts search, taxonomy, post_type, and post_date as fields.
Let’s take a look at another example. This time we will include category, tags, post types, and date fields.
You will need to use the shortcode like this:
Here is how it would look:
Now instead of using drop down select field, you can also use checkboxes and add labels for each field. To do that, you will need to add two more parameters to the shortcode.
If you use a separate page to display your blog posts, then you can add this shortcode to the page.
[searchandfilter headings="Select categories:" types="checkbox" fields="category"]
If you are using multiple fields, types, and headings, then you need to make sure that items are in the same order for each parameter.
Let’s see another example. This time we are adding category, tags, and post type fields with different headings and form field types.
[searchandfilter headings="Post type, Category, Tag" types="checkbox, select, select" fields="post_types,category,post_tag"]
This is how it would appear on your site:
Please refer to plugin’s documentation page for more ways to use the shortcode.
You can also use these shortcodes inside a text widget. If the shortcode doesn’t work in your text widget, then please see our guide on how to use shortcodes in your WordPress sidebar widgets.
We hope this article helped you learn how to let users filter posts and pages in WordPress. You may also want to see these actionable tips to drive traffic to your WordPress site.
The post How to Let Users Filter Posts and Pages in WordPress appeared first on WPBeginner.