Comparison of categories in ACF AJAX repeater fields

Asked

Viewed 23 times

0

Talk personal, all right? I hope so! I’m having a hard time solving the following problem. I have a Post_type Called prize and a taxonomy called winners, in the post I have a repeater field where register winners, I can add name, logo, and category (she is pulled as I register the categories in my taxonomy), They’re all FCA camps! Also, I have a select of years and an ajax request to filter posts per year, so far so good, but at the time of lists the posts need to list all winners of the repeater field following the rule that they will be separated according to their category.

Example: Winner 01 = name: so-and-so: logo = test Category: Seller Winner 02 = name: so-and-so: logo = test Category: Seller Winner 03 = name: so-and-so: logo = test Category: Builder Winner 04 = name: so-and-so: logo = test Category: Builder

*Each Winner with the same category needs to stay in a separate html column.

Column 01 Winner 01 Winner 02

Column 02 Winner 03 Winner 04

My ajax function is this one:

add_action('wp_ajax_nopriv_rankings-filter', 'filter_rankings');
add_action('wp_ajax_rankings-filter', 'filter_rankings');
function filter_rankings()
{
    $html = '';

    $year = $_POST['ano'];
    $args = array(
        'orderby' => 'title',
        'order' => 'ASC',
        'post_status' => 'publish',
        'post_type' => 'top_imobiliario',
        'date_query' => array(
            array(
                'year'  => $year,
            ),
        ),

    );
    $query = new WP_Query($args);
    if ($query->have_posts()) :

        while ($query->have_posts()) : $query->the_post();
            $dateAno = get_the_date('Y');
            $vencedor = get_field('vencedores_do_premio');
            foreach ($vencedor as $vencedores) {
                $nome = $vencedores['nome_do_vencedor'];
                $logo = $vencedores['logo_do_vencedor'];
                $categoria = $vencedores['categoria_do_vencedor'];
                $html .= '<div class="single-vencedor col-md-4">';
                $html .= '<div class="bloco-img-vencedor">';
                $html .= '<img src="'.$logo.'" alt="Logo vencedor">';
                $html .= '</div>';
                $html .= '<h6>' . $nome  . '</h6>'; 
                $html .= '</div>';

            }

            wp_reset_postdata();
        endwhile;

    else :

    endif;

    echo $html;
    exit;
}

1 answer

1


You will have to organize your winners by categories first... and then you should organize your html according to the structure you want...

//Organiza os vencedores em categorias
foreach ($vencedor as $vencedores) {
    $vencedores_categorias[$vencedores['categoria_do_vencedor']][] = array(
        'nome' => $vencedores['nome_do_vencedor'],
        'logo' => $vencedores['logo_do_vencedor']
    );
}

//organiza o HTML
//loop categoria por categoria
foreach ($vencedores_categorias as $categoria => $vencedores_cat) {
    $html .= '<div class="categoria col-md-12"><h2>'.$categoria.'</h2>';
    //loop vencedor por vencedor da categoria
    foreach ($vencedores_cat as $vencedor_cat) {
        $html .= '<div class="single-vencedor col-md-4">';
        $html .= '<div class="bloco-img-vencedor">';
        $html .= '<img src="'.$vencedor_cat['logo'].'" alt="Logo vencedor">';
        $html .= '</div>';
        $html .= '<h6>' . $vencedor_cat['nome']  . '</h6>'; 
        $html .= '</div>';
    }
    $html .= '</div>';
}
  • Great guy, it worked super well. Thank you so much

Browser other questions tagged

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