我有一个允许用户从下拉菜单中选择一个县的表单,然后将我的表单发布到我的functions.php页面,在那里我一直使用IF语句来运行我的查询。
if ($_POST['dropdown1'] == 'option 1' && $_POST['dropdown 2'] == 'option 4' && $_POST['county'] == 'cheshire' ) {
// RUN QUERY
}
然而,我的问题是,在每种可能的情况下,我都不能真实地使用每个县的IF语句,因为会有成千上万的选项,有没有人更好地了解我如何做到这一点?
if ($_POST['vehicleType'] == 'hgv' && $_POST['coverageRegion'] == 'national' ) {
$customkey = 'vehicleType';
$customvalue = $_POST['vehicleType'];
$customkey1 = 'coverageRegion';
$customvalue1 = $_POST['coverageRegion'];
$customkey2 = 'locationType';
$customvalue2 = $_POST['locationType']; $args = array('orderby' => 'meta_value_num', 'meta_key' => 'order', 'order' => 'ASC',
'meta_query' => array(
array(
'key' => $customkey,
'value' => $customvalue,
'compare' => '='
),
array(
'key' => $customkey1,
'value' => $customvalue1,
'compare' => '='
),
array(
'key' => $customkey2,
'value' => $customvalue2,
'compare' => '='
)
) // end of array
); //end of if
$query = new WP_Query( $args);
// The Loop
$i = 0; $i = -1;
while ( $query->have_posts() )
{
$i++;
$query->the_post();
if ( $keys = get_post_custom_keys() )
{
echo "<div class='clearfix card-prod ".($i==0?'first':'')."'><div class='top-dets'><span class='card-title'>";
echo the_title();
echo "</span>";
// Network query
$network_value = get_post_custom_values('srchnetwork');
foreach ( $network_value as $key => $value ) {
echo '<span class="srch-val-">'. $value . '</span>'; }// Pricing Query
$pricing_value = get_post_custom_values('srchpricing');
foreach ( $pricing_value as $key => $value ) {
echo '<span class="srch-val-1">'. $value . '</span>'; }
// Setup Query
$setup_value = get_post_custom_values('srchsetupfee');
foreach ( $setup_value as $key => $value ) {
echo '<span class="srch-val-2">'. $value . '</span>'; }
// Services Query
$services_value = get_post_custom_values('srchservices');
foreach ( $services_value as $key => $value ) {
echo '<span class="srch-val-3">'. $value . '</span></div>'; }
// Big Card Query
$bigcard_value = get_post_custom_values('bigcard');
foreach ( $bigcard_value as $key => $value ) {
echo '<a href="/" class="cardclick"><img src="/wp-content/themes/CAFC/images/cards/'. $value . '" alt="'; }
echo the_title() . '" /></a>';
echo '<img src="wp-content/themes/CAFC/images/top-choice.jpg" alt="Top Choice" class="topchoice">';
echo the_excerpt()."</div>"; }
}
}
答案 0 :(得分:0)
您可以用这种方式编写代码,这样您就不必处理所有可能的选项。
global $wpdb;
$postKeys = array('vehicleType', 'coverageRegion', 'locationType');
$args = array(
'orderby' => 'meta_value_num',
'meta_key' => 'order',
'order' => 'ASC',
'meta_query' => array()
);
foreach ($postKeys as $key) {
$args['meta_query'][] = array(
'key' => $key,
'value' => $wpdb->escape($_POST[$key]),
'compare' => '='
);
}
$query = new WP_Query($args);