Contents
How to filter custom posts in WordPress with Ajax?
Filtering posts, pages, or custom post types with ajax all works in the same way. I assume you have a basic knowledge of javascript and PHP for the syntax and basic WordPress to know how to make a query.
How to create an asynchronous Ajax filter by yourself?
In this post I will show you how to create an asynchronous AJAX filter by yourself, which allows to filter posts by taxonomy terms, meta values and sort results by publish date. I do not recommend to use plugins for these purposes if possible.
Is there a way to filter without page reload in WordPress?
WordPress Filter without page reload using ajax is not a hard thing to do. Many developers will quickly install another plugin like facetwp or yith to do things like this. But it’s actually pretty simple, as WordPress has default ajax functionalities.
How to change taxonomy parameter in WordPress post filter?
You can replace the taxonomy parameter value on line 2 with any custom taxonomy name or post_tag. By the way, it is also possible to combine this code with my multisite plugin, you get it here and network_get_terms () function.
How to create a filter form in WordPress?
Our filter form will consist of 4 parts. I’ll describe each part of the form separately. First part of the form is a dropdown of taxonomies. To create that you can freely use get_terms () function. This function works not only for custom taxonomies but for default categories and tags as well.
How to filter all network posts in WordPress?
So, all network categories will be in the select dropdown and all the network posts will be displayed as the filter search results. I use range of prices. In our case price is a custom field value that is stored in wp_postmeta table under the _price key in database.
Is there a plugin for Ajax in WordPress?
Many developers will quickly install another plugin like facetwp or yith to do things like this. But it’s actually pretty simple, as WordPress has default ajax functionalities. For me, as a front-end developer, the hardest part is making the actual WP_Query if we’re extending this method for combined filters WooCommerce.
Which is the default Ajax function in WordPress?
I use default WordPress function site_url () to get an actual website URL Line #1. admin-ajax.php is the default WordPress AJAX processor. I place it into the form action attribute just for simplicity, you can also get it with admin_url (‘admin-ajax.php’). Line #23.
How to do Ajax queries in WordPress with PHP?
Ajax queries in WordPress with PHP all sounds like a nightmare, but it’s all fun and games! Because we are using the default /wp-admin/admin-ajax.php URL, we can write this function somewhere in the functions.php file: When hitting the ‘filter_projects’ function, we’ll be checking if we did POST our category-slug.
How to create a meta query filter in WordPress?
In our case price is a custom field value that is stored in wp_postmeta table under the _price key in database. I will show you how to create a meta_query filter in step 3 of this tutorial but you can also read more about meta_query usage here. Simple radio buttons will help us to sort posts in ascending or descending order.