V
V
Vladimir Kulikov2020-05-11 13:06:16
WordPress
Vladimir Kulikov, 2020-05-11 13:06:16

How to sort posts by acf field in query_posts from largest to smallest?

Hello! If I have 2 buttons "cheaper" and "more expensive"

<form action="" method="get" >
                            <div class="right">
                                <ul>
                                    <li>Сортировать:</li>
                                    <label class="radio_1">
                                        <input type="radio" name="filter2" value="minimal_price" id="">
                                        <li>Дешевле</li>
                                    </label>
                                    <label class="radio_2">
                                        <input type="radio" name="filter2" value="maximum_price" id="">
                                        <li>Дороже</li>
                                    </label>
                                </ul>
                            </div>
                            <button>го2</button>
                        </form>


There is a query_posts cycle for displaying posts, I have already done filtering by price. But how can I expand it so that the loop displays cheap products first by the "price" field?
<?php 
                    $query = new WP_Query( array(
                        'post_type' => 'post', 
                        'posts_per_page' => 6, 
                        'meta_query' => array(
                            'max_price' => array(
                                'key'     => 'цена',
                                'value'   => $max_price,
                                'type' => 'NUMERIC',
                                'compare' => '<=',
                            ),
                            'min_price' => array(
                                'key'     => 'цена',
                                'value'   => $min_price,
                                'type' => 'NUMERIC',
                                'compare' => '>=',
                            ),
                        ),
                        'orderby' => 'max_price,min_price',
                    ) );
                ?>
                <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                    <?php
                    get_template_part( 'loop/item_type1', 'loop' );
                    ?>
                <?php endwhile; ?>
                <?php
                    wp_reset_query();
                ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kulikov, 2020-05-11
@it_proger29

'orderby'   => 'meta_value_num',
                        'meta_key'  => 'цена', 
                        'order' => 'DESC'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question