Filtering get_terms loop by custom fields & custom taxonomy

Question

On specific page I’m displaying posts from my CPT („opencourses”). Posts from this specific CPT are grouped by custom taxonomy („kategoria”) – this piece of code works. At the same page I have four different select fields [dropdowns] to filter the query/loop. One dropdown is dedicated to filtering by mentioned taxonomy, rest of them are related to an ACF epeater subfields. I’ve managed to populate select fields with proper data – this works too.

What I’m missing is one thing – when I’m filtering my posts, it shows entire group of posts [meaning taxonomy and its terms] instead THE SPECIFIC post/term.

Hopefully below example will be more descriptive – what I get currently:

TAXONOMY_CHILD_1

  • Post 1
  • Post 2
  • Post 3
  • Post 4

What I want to get:

TAXONOMY_CHILD_2

  • Post 3

It’s crucial to me just to highlight the specific posts. Taxonomy Childs are in my example types of courses. Inside specific type I have bunch of courses with different timing, location, price, so showing them at the same time is pointless.

Current code:

<div class="listaWarsztatow" id="listaWarsztatow">
<?php

    $taxonomy = 'typ';

        if (is_page(29557) ):
            $childno = '205';

        elseif (is_page(29640) ):
            $childno = '206';

        endif;                                      

    $args = array(
        'child_of'=> $childno,
        'childless' => true                     
    );

    $taxonomy_terms = get_terms($taxonomy, $args);


    if($taxonomy_terms) {
        foreach($taxonomy_terms as $taxonomy_term) {

            $taxquery = array('relation' => 'AND');
            $metaquery = array('relation' => 'AND');

            if(isset($_GET['obszar']) && $_GET['obszar'] != '')
            {
                $taxquery[] =  array(
                    'taxonomy' => 'kategoria',
                    'field'    => 'slug',
                    'terms'    => $_GET["obszar"],
                    );
            }
            if(isset($_GET['firma']) && $_GET['firma'] != '')
            {
                $metaquery[] =  array(
                    'compare' => '=',
                    'key'    => 'terminy_warsztatow_$_firma',
                    'value'    => $_GET["firma"],
                );
            }
            if(isset($_GET['lokalizacja']) && $_GET['lokalizacja'] != '')
            {
                $metaquery[] =  array(
                    'compare' => '=',
                    'key'    => 'terminy_warsztatow_$_wojewodztwo',
                    'value'    => $_GET["lokalizacja"],
                );
            }
            if(isset($_GET['data']) && $_GET['data'] != '')
            {
                $metaquery[] =  array(
                    'compare' => '=',
                    'key'    => 'terminy_warsztatow_$_data',
                    'value'    => $_GET["data"],
                );
            }

            $args_main_query = array(
                'post_type' => 'opencourses',
                "$taxonomy" => $taxonomy_term->slug,
                'post_status' => 'publish',
                'posts_per_page' => -1, 
                'fields' => 'all',
                'tax_query' => $taxquery,
                'meta_query' => $metaquery,
            );

            $query = new WP_Query( $args_main_query );

            if ( $query->have_posts() ) : ?>

            <?php $color = get_field('blok_szkoleniowy_kolor', $taxonomy_term); ?>

            <div class="term_anchor" id="<?php echo $taxonomy_term->slug; ?>"></div>
            <h4 class="term_blok_szkoleniowy"><span class="spacer" style="background:<?php echo $color; ?>"></span><?php echo $taxonomy_term->name; ?></h4>

                <?php while ( $query->have_posts() ) : $query->the_post(); ?>

                <div class="row">
                    <div class="col-md-12">
                        <div class="coursWrapper">

                            <div class="visibleInfoCourses" style="border-left: 5px solid <?php echo $color; ?>">
                                    <div class="row">
                                        <div class="col-sm-7">
                                            <p><a href="<?php the_permalink() ?>"><?php the_title() ?></a>
                                            <?php if (get_field('nowosc')) : ?><span class="labelItem labeImNew">Nowość</span><?php endif ?>
                                            <?php if (get_field('new_form')) : ?><span class="labelItem labeImNewForm">Nowa formuła</span><?php endif ?>
                                            <?php if (get_field('new_date_info')) : ?><span class="labelItem labeImNewDate">Zmiana terminu</span><?php endif ?>
                                            </p>
                                        </div>
                                        <div class="col-sm-5">
                                            <div class="buttonsHereRight">
                                                <button class="btnBorderGrey btn-lean-sm">Terminy warsztatów</button>
                                            </div>
                                        </div>
                                    </div>                                                                                                      
                            </div>

                            <div class="InvisibleInfoCourses" style="border-left: 5px solid <?php echo $color; ?>">
                                <?php if (have_rows('terminy_warsztatow')) : $i = 0; ?>
                                    <div class="TerminySzkolen" style="padding: 0;">
                                        <?php while (have_rows('terminy_warsztatow')) : the_row();
                                            $i++;
                                            $hotele = get_sub_field('polecane_hotele');
                                            $zapisy = get_sub_field('stan_zapisow'); ?>
                                            <div class="InvisibleInfoCoursesDetails" <?php if ($zapisy == 'Sprzedane') : echo " style='opacity:.6;'";endif; ?>>
                                                <div class="row">
                                                    <div class="col-sm-10">
                                                        <span><img src="<?php the_sub_field('logotyp') ?>" alt="<?php the_sub_field('lokalizacja') ?>"></span>
                                                        <span><i style="opacity: .6;" class="glyphicon glyphicon-time"></i><?php the_sub_field('data') ?></span>
                                                        <span><i style="opacity: .6;" class="glyphicon glyphicon-map-marker"></i><?php the_sub_field('lokalizacja') ?></span>
                                                        <span><i style="opacity: .6;" class="glyphicon glyphicon glyphicon-list-alt"></i><?php echo $zapisy; ?></span>
                                                    </div>
                                                    <div class="col-sm-2">
                                                        <div class="buttonsHereRight">
                                                            <!-- <a class="btn-lean btn-lean-sm" target="_blank" download="Formularz zgłoszeniowy" href="<?php the_sub_field('przycisk') ?>" onclick="gtag('event', 'szkolenia', {'event_category' : 'szkolenie-otwarte-lista-szkoleń','event_label' : '<?php the_title() ?> '});" <?php if ($zapisy == 'Sprzedane') : echo " style='display:none'";endif; ?>>Zapisz się</a> -->
                                                            <!-- <a class="btn-lean btn-lean-sm" href="<?php the_permalink(); ?>" onclick="gtag('event', 'szkolenia', {'event_category' : 'szkolenie-otwarte-lista-szkoleń','event_label' : '<?php the_title() ?> '});" <?php if ($zapisy == 'Sprzedane') : echo " style='display:none'"; endif; ?>>Zapisz się</a> -->
                                                            <a class="btn-lean btn-lean-sm" href="<?php the_permalink(); ?>" <?php if ($zapisy == 'Sprzedane') : echo " style='display:none'"; endif; ?>>Zapisz się</a>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        <?php endwhile; ?>
                                    </div>
                                <?php endif ?>
                            </div>
                        </div>
                    </div>
                </div>

                <?php endwhile; ?>



            <?php wp_reset_postdata(); // so nothin' weird happens to other loops
            endif;
        }
    }

?>
</div>
0
, , , , Pawel Skaba 2 years 2020-01-08T08:40:14-05:00 0 Answers 84 views 0

Leave an answer

Browse
Browse