簡體   English   中英

按 ACF 轉發器子字段和自定義分類法過濾 wp_query

[英]Filter wp_query by ACF repeater subfields and custom taxonomy

在特定頁面上,我顯示來自我的 CPT(“公開課程”)的帖子。 來自此特定 CPT 的帖子按自定義分類法 (“kategoria”) 分組。 這部分有效。 在同一頁面上,我有四個不同的選擇字段來過濾查詢。 一個下拉菜單專門用於按提到的分類法過濾,其余的與轉發器子字段相關。 我已經設法用正確的數據填充選擇字段。 這部分也很好用。

我缺少的是一件事——當我過濾我的帖子時,它會顯示整個帖子組而不是特定的帖子。 希望下面的例子更具描述性——我目前得到的:

TAXONOMY_CHILD_1

  • 發布 1
  • 帖子 2
  • 帖子 3
  • 帖子 4

我想得到什么:

TAXONOMY_CHILD_1

  • 帖子 3

僅突出顯示特定帖子對我來說至關重要。 Taxonomy Childs 是我的示例課程類型。 在特定類型中,我有一堆不同時間、地點、價格的課程,所以同時展示它們是沒有意義的。

完整代碼可以在這里找到:

<?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;
        }
    }

?>

請嘗試:

   //Since the changed behaviour of esc_sql() in WordPress 4.8.3, 
   //cannot use the % character as a placeholder, hence need to alter 'where' close:
   function textdomain_posts_where( $where ) {
        $where = str_replace("meta_key = 'terminy_warsztatow_$", "meta_key LIKE 'terminy_warsztatow_%", $where);
        return $where;
   }
   add_filter('posts_where', 'textdomain_posts_where');



  if (isset($_GET["filtr_firma"]))
    {
        $meta_query[] =  array(
            'compare' => 'LIKE',
            'key'    => 'terminy_warsztatow_$_firma',
            'value'    => $_GET["filtr_firma"],
        );
    }
    if (isset($_GET["filtr_lokalizacja"]))
    {
        $meta_query[] =  array(
            'compare' => 'LIKE',
            'key'    => 'terminy_warsztatow_$_data',
            'value'    => $_GET["filtr_lokalizacja"],
        );
    }
    if (isset($_GET["filtr_termin"]))
    {
        $meta_query[] =  array(
            'compare' => 'LIKE',
            'key'    => 'terminy_warsztatow_$_wojewodztwo',
            'value'    => $_GET["filtr_termin"],
        );
    }

    ...


因為你的repeater key以形式存儲在數據庫中

terminy_warsztatow_0_firma
terminy_warsztatow_0_data
terminy_warsztatow_0_wojewodztwo
---
terminy_warsztatow_1_firma
terminy_warsztatow_1_data
terminy_warsztatow_1_wojewodztwo
---
terminy_warsztatow_2_firma
terminy_warsztatow_2_data
terminy_warsztatow_2_wojewodztwo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM