Filtering posts by creation date wordpress plugin. Beautiful Taxonomy Filters: Taxonomy filters for custom post types in WordPress
Hello! We continue to analyze the most interesting and most useful plugins for Woocommerce online store! Today you will learn how to add to your website ajax filter woocommerce products. You can search for products by filters - category, price, size, color, tag, sale, in stock, title, SKU. You can specify your own headers for filters. Can be hidden certain elements. You can display products in filters as a link or a checkbox. You can choose the filter display style, vertical or horizontal. You can specify the number of columns, etc.
You can install the plugin directly from WordPress admin panels. Go to page: Plugins – Add new, enter the name of the plugin in the search form, press Enter, install and activate the plugin.
Next, after installing and activating the plugin, go to the page: . This page will display all created filters. To create new filter click on the button above - Add new.
– Form Title, specify the name of the filter.
– Layout, select the filter display style.
– Empty Fields, do not show empty fields.
– Product Sorting, hide product sorting.
– Product Count, hide the display of the number of products.
– Pagination, hide product pagination.
– Products Per Page, the number of products displayed on one page when pagination is enabled.
– Toggle Field Groups, displays the toggle for groups.
– Scroll To Result, enable scrolling.
– Result Page Template, show results on the same page or on a separate page.
Next, you need to use your mouse to drag the filters you need into the lower field. Each filter can be customized, specify the filter name and configure additional options. At the end, to save the created filters, click on the button - Save.
Few people know, but WordPress has very good opportunity assign custom fields (better known as meta data) to any post. Essentially, it is the most powerful component of posts and content types in WordPress, which is rarely used by beginner users. With this tutorial, you will be able to better understand how you can use custom fields in WordPress.
What are custom fields for posts in WordPress
Custom fields is meta data that is specified as key/value pairs, where “key” is a name that identifies a specific field.
Thus, the key is permanent and remains the same for all posts, but can be used several times within the same post. The value is the information displayed in the field when you call it in your WordPress theme, and can change for each post.
Using Custom Fields in WordPress
Let's look at a very simple example of meta data in action. Create new entry and fill it with content. After that, click on the “Screen Settings” button and check the “Custom Fields” option. A corresponding block with a form to fill out should appear under the content.
Let's use this form to show the author's mood at the time of writing the article. Click "Add New Field" and fill in the Name/Value lines like Today's Mood/Happy. Once you add this field and save the post, additional information, associated with the article, will be recorded in the database. It can be displayed anywhere in your theme.
Since this is a very simple example, we will use the simplest way to display this information. Let's say you want to show your mood before a person reads your post. To do this, go to the editor appearance and open the file "single.php". Find there a "loop" starting with
while (have_posts()) : the_post();
and add the following code in front of it:
Now, the article page will display something like this:
Today's mood: Happy
This display method is useful when you only have one field, but if you have 5 of them and you want to show them in 5 different places? Then it is easier to use the “get_post_meta” hook. Let's say you want to display only one meta data field in a separate place in the theme. You need to add the following code before the "loop" loop:
ID, "Today's mood", true); ?>
What if you have additional meta data, for example for the songs you were listening to while writing the post? In this case, you can create several Song keys that have different meanings. And then insert similar code before the loop:
ID, "Songs", false); ?>
This post is inspired by music:
- ".$track." ";
echo "
} ?>
note that important role The third parameter “false” plays here. It indicates that the function will return an array of values for given key. This is very convenient way to display multiple values for one field.
Advanced Custom Field Features in WordPress
The use of metadata can go far beyond showing mood and song. For example, some popular plugins, such as Yoast's WordPress SEO and WP-Post Ratings, as well as many others, use metadata to implement their functionality.
Let's look at a few advanced metadata techniques that you may find useful as you work on your site. This is only a small part of their application, but it works well as an example.
Filter records by custom fields
WordPress has a lot of good function“query_posts”, which, among other things, has an option for custom fields. So we can use it to create a sort of filter to display records with a specific key/value. All you need to do is insert the “query_posts” tag above the “loop” statement.
Let's say you want to filter all articles to show only those you wrote while listening to music. To create such a filter, you need to insert the following query before starting the loop:
query_posts("meta_key=Songs");
Thanks to this code, only certain articles with the meta key “Songs” will be displayed, and the rest will be filtered. So you can create new page, paste into it this code, and it will contain all records that have such a key.
And if, let’s say, you want to configure the filter a little more to display on the page only those posts that were written while listening to a track by a certain artist. Then you will need to insert the following code into the template:
query_posts("meta_key=Songs&meta_value=Metallica");
Searching for records using custom fields
Filtering posts by metadata is certainly a convenient thing, but creating a page for each artist so that the user can see articles written while listening to him is absurd. In such a situation, it would be better to provide the user with the opportunity to search for a given word.
Unfortunately, standard search WordPress does not take into account additional data, but only the title and content of the post. But this problem can be solved by improving the search using the Custom Fields Search by BestWebSoft plugin. This plugin allows you to add any custom field to the standard WordPress search. All you need to do is select the fields you want the search to filter through.
Using this plugin is quite simple. Once you install and activate it, in the menu administrative panel The “BWS Plugins” item will appear, opening the plugin settings page. This page will display a list of all the meta data created on your site. All you have to do is select the required fields so that the standard search takes them into account, and save the result.
Hide the area for adding fields
Often when you are setting up a site for clients who are not very "tech savvy" in this matter, it would be very appropriate to remove the area for adding meta data and replace it with more user-friendly interface. This is especially important when there are many options for customizing meta data and finding the right ones becomes very difficult.
Removing them is quite simple. All you need to do is open your theme's "functions.php" file and add the following lines there:
add_action("admin_init","remove_custom_meta_boxes");
function remove_custom_meta_boxes() (
remove_meta_box("postcustom","post","normal");
remove_meta_box("postcustom","page","normal");
}
Conclusion
After reading this article, you should have general idea about using meta data in WordPress. But you must understand that they are only limited by your imagination, there are many ways to use them that can be found in plugins and themes everywhere.
Everyone knows that the default search in WordPress is not very good. However, allowing users to filter results by categories and tags, custom taxonomies, or post types can help them better navigate their search for content. We have previously shown how to add a similar advanced search form in WordPress. In this article we will show you how to add an Ajax taxonomy filter to WordPress search.
Why use an Ajax taxonomy filter in WordPress search?
Ajax search is faster and offers an improved user experience. For example, a car sales site might have custom taxonomies for various types cars, price ranges, models, etc. Such an interface will help the user find exactly what he needs. It will also help site administrators better organize their content so that it, in turn, is better found by users.
First of all, you need to install and activate the Ajax WP Query Search Filter plugin. Once activated it will add new element“Ajax WPQSF” menu in the sidebar of the site’s administrative panel. Clicking on this menu will take you to the plugin page where you can click on the button Add New Search Form to create your own search form.
You'll need to provide a title for your form first to make it easier to identify as you add other forms using this plugin. Next, you can select the types of records that this search form will process. You will see standard for WordPress Posts and pages along with the custom post types your site has.
The next setting is choosing a taxonomy. You can select logical dependencies between taxonomy queries. For example, if the user selects flowers and animals, you can tell the plugin whether to process both queries at the same time or to display the result even if a result is found for one of the taxonomies. Next, you need to add a taxonomy by clicking on the button Add Taxomy.
A new pop-up window will appear and you will see the category and any other custom taxonomy you have on your site. You can add a label for the taxonomy, you can select the option to hide empty terms. And finally, select the display type. You can display them in the form of checkboxes, drop-down menus or radio buttons. After selecting a custom taxonomy, you can further customize your search form by adding custom meta fields in exactly the same way as you added the taxonomy.
The next tab allows you to customize search results and other parameters. On this tab you can specify the default word for the search form. If the user selects a taxonomy or a custom meta field and enters keyword. then the plugin will perform a search query for the word and ignore the user’s choice of taxonomy and custom meta field.
That's it. Now save your search form and you will see the shortcode. It can be added to any post, page, or text widget on your site to display a search form.
We hope this article helped you add an Ajax taxonomy filter in WordPress search to your site. Using this advanced search and sorting feature can help you create simple but very effective sorting and searching filters on your WordPress site.
It is often necessary not to display all categories on the main page wordpress blog. You need to filter posts by category. There are two options here, the first filter is not to display records of some categories, and the second is to display records of only selected categories. I think more often you have to deal with the first filter. I wanted to filter the posts certain category, filled only background information. Why create a bunch of short posts on home page, besides, in this pile there will also be thematic articles, and for them I have more categories than categories with background information. So I’ll filter the posts of one category from the output on the main page and that’s it.
There is a wonderful function in WordPress that changes the standard query when looping through the database. This is the query_posts() function; it takes a lot of parameters that affect the request and, as a result, the output of posts to the page. I need to specify a category that does not need to be displayed. There are several options for these purposes:
- parameter category__not_in - records not included in the specified category;
- parameter cat - category id.
I'll choose the one that's shorter.
Let's write the function for our case:
query_posts(array(‘cat’ => -1));
You need to indicate the values of categories that will not be included in the output, separated by commas. Place this code above wordpress cycle on the main page "index.php" and entries from specified categories will not be displayed.
-1, -5));?>
This code will filter categories 1 and 5, which will not be displayed on the main page.
Let's launch and check.
Everything works - records of the specified categories are not found in the output. But there is one BUT!
At the bottom of the page, below the posts, is page navigation. In the admin panel you can limit the number of entries displayed on the page, and to see the remaining entries you will have to use this navigation. I just couldn’t do it, I click on the navigation links, but the articles are still the same. The quest has begun, the final prize of which is a solution to the problem with page navigation. I didn’t immediately guess why this behavior was happening. After two hours of searching, it turns out that this very call to the query_posts() function is the reason for this behavior of page navigation. But I really need to remove categories with reference information and... The quest continues.
Whether long or short... I thought about postponing the search for a solution. How I got hooked on the ‘paged’ parameter. He is responsible for page navigation and to normalize the navigation, you need to set this parameter to get_query_var('paged'), which indicates the current page number in the paged navigation.
As a result, we get a function that overrides 2 parameters:
-1, "paged" => get_query_var("paged")));?>
Close the first category for display and set the ‘paged’ parameter.
Now the code works as expected.
(Total visitors: 1,134, today: 1)
The presence of a search engine on a site has a positive effect on its visits. After all, in this way the visitor can quickly find the information he needs, even if there is good navigation or site map.
Among standard tools WordPress available necessary system search, which in principle can be displayed in any part of the site. Of course, this is very convenient, since this flexibility can be used when developing any theme. But there is one point. The standard search engine does not allow you to filter the search results in any way. But it would be nice to have this option, especially if the site has a lot of categories or tags.
How to add a search filter in WordPress?
One way to solve this problem is to use a free WordPress plugin Search & Filter.
Search & Filter plugin
WITH using Search& Filter visitors to your site will be able to search by categories, tags, custom taxonomy, post type, publication date, or any combination thereof to really improve search queries. Filter fields can appear as drop-down menus, checkboxes, radio buttons, or multiple selections.
First, the plugin needs to be installed and activated. After activation, the plugin will add a new menu item Search & Filter into the panel WordPress admin. Clicking on it will take you to the plugin usage page with detailed documentation on how to use it.
It should be noted that the plugin does not add search filters to the site in automatic mode. To do this, it offers special shortcodes that accept different parameters to display filtering options. You can use them anywhere on the site, including widgets, which provides flexibility in display and customization.
For example, to display a drop-down list of categories and tags in the search box, you can use the following shortcode:
To insert it anywhere in the WordPress template use service team do_shortcode(), which displays the contents of the shortcode on the page. It will look like:
If you add this code to the file search.php, then the search page will look like this:
Let's look at another example that includes categories, tags, post types, and date.
Now, instead of using a dropdown selection field, you can also use checkboxes and add labels for each field. To do this, we need to add two more parameters to our shortcode.
If you are using separate page to display your blog posts, you can add this short code to the page.
If you use multiple fields, types, and headers, you need to make sure that the elements are in the same order for each parameter.
Let's see another example. This time we will add categories, tags and post fields with different headings and types of form fields.
These shortcodes can also be used in widgets.