【发布时间】:2019-03-29 18:58:03
【问题描述】:
在我正在构建的 wordpress 网站中,我有一个自定义帖子 (cp_course),其中包含以下子字段:course_code、course_duration、period_a_start、period_a_end、period_b_start、period_b_end、@ 987654328@、period_c_end、period_d_start 和 period_d_end。
搜索表单具有以下字段:code、duration、datefrom、dateto。用户可以使用其中任何一个进行搜索。我只实现了用户使用所有搜索字段的情况:
if(!empty($code) && !empty($duration) && !empty($datefrom) && !empty($dateto))
{
$args = array(
'post_type' => 'cp_course', 'numberposts' =>-1,'orderby' => 'ID', 'order' => 'ASC', 's' => $searchterm,
'meta_query' =>
array(
'relation' => 'AND',
array(
'key' => 'course_code',
'value' => $code,
),
array(
'key' => 'course_duration',
'value' => $duration,
),
array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'period_a_start',
'compare' => '>=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($datefrom)),
),
array(
'key' => 'period_a_end',
'compare' => '<=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($dateto)),
),
),
array(
'relation' => 'AND',
array(
'key' => 'period_b_start',
'compare' => '>=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($datefrom)),
),
array(
'key' => 'period_b_end',
'compare' => '<=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($dateto)),
),
),//IF I REMOVE the follow lines (period_c_* and period_d_*) then it works
array(
'relation' => 'AND',
array(
'key' => 'period_c_start',
'compare' => '>=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($datefrom)),
),
array(
'key' => 'period_c_end',
'compare' => '<=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($dateto)),
),
),
array(
'relation' => 'AND',
array(
'key' => 'period_d_start',
'compare' => '>=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($datefrom)),
),
array(
'key' => 'period_d_end',
'compare' => '<=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($dateto)),
),
),
),
)
);
$course = get_posts($args);
}
<div class="page_title">
<h3>Courses</h3>
</div>
<?php foreach ($course as $post):setup_postdata($post);?>
<a href="#"><?php the_title();?></a>
<?php endforeach;wp_reset_postdata();?>
但它似乎不起作用。它卡在一个循环中,已经 10 分钟并且仍在搜索。出了什么问题?
有没有比多个ifs更简单的逻辑来实现不同的情况?我将使用这样的东西,但它看起来真的很长而且很复杂......:
if(!empty($code) && !empty($duration) && !empty($datefrom) && !empty($dateto))
{....}
else if(empty($code) && !empty($duration) && !empty($datefrom) && !empty($dateto))
{....}
.......
【问题讨论】:
标签: php wordpress search filter advanced-custom-fields