A
A
AeRB2020-01-22 04:18:06
WordPress
AeRB, 2020-01-22 04:18:06

How to filter WordPress posts by taxonomy using AJAX checkboxes?

Hello, I'm posting here because I'm facing a problem that I can't solve. On the site of an online store on Woocommerce, there is a page of brands, and on it there is a checkbox block with tags and a block with brand logos
5e27a339a8ca4742737308.png
There is a task from the customer

For example, using filters, the user can choose to display brands that only produce sofas and chairs. The filter works like a checkbox (multiple options can be selected). Active filter items - with a black background. If no filter is selected, then the entire list of brands is displayed.
Bold indicates the number of brands currently displayed on the page in bold.
When changing filters, the figure should change.
Filtering and changing the number in the title should occur without reloading the page.

I understand that it should work through AJAX + I tried a bunch of plugins that did not help me. Rummaged through a lot of information and all past. Maybe there are kind people who will help me or at least "put me on the right path"?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly Kulikov, 2020-01-22
@AeRB

Perhaps the easiest option is to write a special route for REST, where to pass this data, receive a json response in response, and use js to display the results on the page.

G
Genri_Rus, 2020-01-23
@Genri_Rus

Here is an example: https://rudrastyh.com/wordpress/ajax-post-filters.html
First you need to link products with brands
In the admin panel, in the products tab, there is a subsection attributes
Create in Brands attributes and already in brands create your brand names
After To do this, you need to dynamically display the brands created on the page in the form of checkboxes, specify their id in the form of a slug of your brands (for the future ajax'a)
I.e. for example, if you have 5 brands, then only 5 checkboxes are displayed, if 3, then only three.
Further, such a request (where you want to display this data)

$args = array(
  'posts_per_page' => -1
        'tax_query' => array(  
           'taxonomy' => тут название вашего созданного атрибута (можно посмотреть в url строке, там будет что-то типа taxonomy='brends'),
           'field' => 'slug',
           'terms' => $_POST['тут что хранится в id или name ваших чекбоксов']
       )
);
$brends = new WP_Query( $args );

And then the cycle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question