【问题标题】:Filter wp_query by ACF repeater subfields and custom taxonomy按 ACF 转发器子字段和自定义分类过滤 wp_query
【发布时间】:2020-04-20 16:17:57
【问题描述】:

在特定页面上,我正在显示来自我的 CPT(“opencourses”)的帖子。此特定 CPT 的帖子按自定义分类法(“类别”)分组。这部分有效。在同一页面上,我有四个不同的选择字段来过滤查询。一个下拉列表专门用于按提到的分类进行过滤,其余的与转发器子字段相关。我已经设法用适当的数据填充选择字段。这部分也很好用。

我缺少的是一件事——当我过滤我的帖子时,它会显示整个帖子组,而不是特定的帖子。希望下面的例子更具描述性——我目前得到的:

TAXONOMY_CHILD_1

  • 发布 1
  • 发布 2
  • 发布 3
  • 发布 4

我想得到什么:

TAXONOMY_CHILD_1

  • 发布 3

突出显示特定帖子对我来说至关重要。分类学儿童是我的示例课程类型。在特定类型中,我有一堆不同时间、地点、价格的课程,所以同时显示它们是没有意义的。

完整的代码可以在这里找到:

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

?>

【问题讨论】:

    标签: php wordpress advanced-custom-fields custom-taxonomy


    【解决方案1】:

    请尝试:

       //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"],
            );
        }
    
        ...
    
    
    

    因为你的中继器密钥以表格形式存储在数据库中

    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
    

    【讨论】:

    • 它可以部分工作。为什么是部分?如前所述,我有 4 种过滤器。当我从下拉列表中选择单个过滤器中的任何值时,提交后的 URL 中将有其他过滤器的空白值。并基于该查询返回零结果。当我自己删除“无效”过滤器并仅保留我选择的过滤器时,它将以某种方式起作用。但它不会只显示单个帖子,而是一组帖子(其中有一个我正在寻找的帖子)。看一看:lean.colorfill.pl/szkolenia-otwarte/szkolenia-dla-produkcji
    猜你喜欢
    • 2017-05-10
    • 2019-05-22
    • 2015-07-14
    • 2020-12-13
    • 1970-01-01
    • 2021-01-03
    • 2016-01-11
    • 1970-01-01
    • 2019-01-21
    相关资源
    最近更新 更多