Creating filters using meta_query and ACF (Advanced Custom Fields)

Asked

Viewed 77 times

0

I have this code in my Wordpress template, my goal is to make a filter functionality using the custom Fields of the ACF plugin as the search parameter of my Custom Post Type property Currently he is listing correctly when only 1 select the filter has some option checked, two or more it does not display any post. The sorting also doesn’t work, I researched about nesting array but it didn’t work in the example I read. Placed ... where the same order is repeated, only with different values to not get too much code here.

    <?php get_header(); ?>
    <?php /*Template Name: Busca ACF*/ ?>
    <header class="page-header">
        <h2>Faça sua busca</h2>
    </header>
    <div class="search-page">
        <aside class="filter">
            <form method="GET">
                <button type="submit">Filtrar</button>
                <select name="orderby" id="">
                    <option value="" disabled selected>Ordenar por:</option>
                    <option value="">Todos</option>
                    <option value="ASC">Menor valor</option>
                    <option value="DESC">Maior valor</option>
                </select>
                <div>
                    <h3>Tipo de contrato</h3>
                    <select name="contrato" id="">
                        <option value="" selected disabled>Selecione uma opção</option>
                        <option value="aluguel">Aluguel</option>
                        <option value="venda">Venda</option>
                    </select>
                </div>
                ...

And this code that lists the Custom Post Type

<div class="properties">
        <?php 
            $args = array(
                'post_type' => 'property',
                'posts_per_page' => -1,
                'meta_key' => 'valor',
                'orderby' => 'meta_value_num',
                'order' => $_GET['orderby'],
                'meta_query' => array(
                    'relation' => 'AND',
                    array(
                        array(
                            'key'   => 'contrato',
                            'value' => $_GET['contrato'],
                            'compare' => '='
                        ),
                        array(
                            'key'   => 'tipo',
                            'value' => $_GET['tipo'],
                            'compare' => '='
                        ),
                        ...
$query = new WP_Query($args);
            if($query->have_posts()) : while($query->have_posts()) : $query->the_post()
        ?>
        <div class="property">
            <a href="<?php the_permalink(); ?>">
                <img src="<?php the_field('destaque'); ?>" alt="<?php the_title(); ?>">
            </a>
            <strong>Código: <?php the_ID(); ?></strong>
            <a href="<?php the_permalink(); ?>">
                <h2><?php the_title(); ?></h2>
            </a>
            <h2>R$ <?php the_field('valor'); ?></h2>
        </div>
        <?php endwhile; else : ?>
        <div class="property">
            <h2>Nenhum resultado encontrado</h2>
        </div>
    <?php endif; ?>
    </div>
</div>
<?php get_footer(); ?>
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.