嘿SO社区(也祝新年快乐!),>
所以我正在研究wp_query的过滤功能。该查询向我们提供了CPT中所有帖子的列表(称为“开放课程”)。和查询工作。当我要通过以下三个条件过滤wp_query时,会出现此问题:三个不同的sub_fields和一个自定义分类法。有关发布信息-一些基本但重要的数据:
整个循环看起来像:
<?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) { $tax_query = array('relation' => 'AND'); $meta_query = array('relation' => 'AND'); if (isset($_GET["filtr_so"])) { $tax_query[] = array( 'taxonomy' => 'kategoria', 'field' => 'slug', 'terms' => $_GET["filtr_so"], ); } if (isset($_GET["filtr_firma"])) { $meta_query[] = array( 'compare' => 'LIKE', 'key' => 'firma', 'value' => $_GET["filtr_firma"], ); } if (isset($_GET["filtr_lokalizacja"])) { $meta_query[] = array( 'compare' => 'LIKE', 'key' => 'wojewodztwo', 'value' => $_GET["filtr_lokalizacja"], ); } if (isset($_GET["filtr_termin"])) { $meta_query[] = array( 'compare' => 'LIKE', 'key' => 'data', 'value' => $_GET["filtr_termin"], ); } $args_main_query = array( 'post_type' => 'opencourses', "$taxonomy" => $taxonomy_term->slug, 'post_status' => 'publish', 'posts_per_page' => -1, 'tax_query' => $tax_query, 'meta_query' => $meta_query, ); $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; } } ?>
已知的问题是什么?-当我使用以下代码进行非常基本的过滤(基于自定义分类法)时:
if( isset($_GET["filtr_so"]) ) { $args_main_query['tax_query'] = array( array( 'taxonomy' => 'kategoria', 'field' => 'slug', 'terms' => $_GET["filtr_so"], ) ); }
效果很好。添加下三个过滤器(按子字段)后,所有过滤器均不起作用。
嘿,所以社区(还有新年快乐!),所以我正在研究wp_query的过滤功能。该查询向我们提供了CPT中所有帖子的列表(称为“开放课程”)。和查询工作。问题...
请尝试:
//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"],
);
}
...