我使用$\\u POST表单从表单中获取多个元素。主要问题是当我有多个“if语句”时。当我想检查是否存在多个字符串时,嵌套语句可能会变得一团糟。。。
$cat = // Categories
if(!empty($cat)) {
$args = array(
\'post_type\' => \'domains\',
\'post_status\' => \'publish\',
\'tax_query\' => array(
array (
\'taxonomy\' => \'domain_categories\',
\'field\' => \'slug\',
\'terms\' => $cat, // ELEMENT EXISTS FOR $cat
),
),
);
} else { // ELSE STATEMENT IF $cat DOESN\'T EXIST
$args = array(
\'post_type\' => \'domains\',
\'post_status\' => \'publish\',
);
} // END STATEMENT FOR $cat
所以,我希望能够简化这一点。
SO网友:user3492770
正如这里所说:Conditional arguments for WP_Query and tax_query depending on if $somevar has a value
我可以在WP\\u查询实例化之外定义参数:
<?php
$tax_query = array(\'relation\' => \'AND\');
if (isset($search_course_area))
{
$tax_query[] = array(
\'taxonomy\' => \'course-area\',
\'field\' => \'id\',
\'terms\' => $search_course_area
);
}
if (isset($search_course_level))
{
$tax_query[] = array(
\'taxonomy\' => \'study-levels\',
\'field\' => \'id\',
\'terms\' => $search_course_level
);
}
if (isset($search_course_mode))
{
$tax_query[] = array(
\'taxonomy\' => \'course-mode\',
\'field\' => \'id\',
\'terms\' => $search_course_mode
);
}
$query = new WP_Query(
array(
//Retreive ALL course posts
\'post_type\' => \'course\',
\'posts_per_page\' => \'-1\',
//Filter taxonomies by id\'s passed from course finder widget
\'tax_query\' => $tax_query,
)
);
?>