K
K
Konstantin2021-06-21 12:25:18
WordPress
Konstantin, 2021-06-21 12:25:18

How to implement post filter using select?

I have a category where posts are displayed. Select pages with city districts are set on the pages, it is necessary that when choosing a district, posts are displayed only with this district.

Each post has additional fields with the help of ACF where these areas are indicated.

The question is how to display these same posts using select ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Gorbunov, 2021-06-22
@gradk

Create a select that, when changed, will add a disctict parameter to the get url, for example, with a district number of 1,2,3

<select onchange="location=value">
        <option value="" selected="selected">Выбрать район</option>
        <option value="?district=1">Район 1</option>
        <option value="?district=2">Район 2</option>
        <option value="?district=3">Район 3</option>
    </select>

In php code, display the districts (let's say they are in the districts post type) with a query, for example
$disctricts = new WP_Query(['post_type' => 'disctricts', 'meta_key' => 'disctrict', 'meta_value' => $_GET['disctrict']]);

 // далее удобным вам способом выводите районы из массива $disctricts

where meta_key is your field in ACF, and in meta_value you intercept the get parameter with the district number.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question