WP_QUERY和TAX_QUERY的条件参数取决于$Somevar是否有值

时间:2012-06-20 作者:Craig Pearson

我已经为“课程”的自定义帖子类型创建了一个新查询。然后使用tax\\u query过滤这些结果,查询3个与1个或多个术语ID匹配的自定义分类法。这些分类法是从搜索页面传递的。

以下是迄今为止正在运行的代码:

// Lets emulate the posted ID\'s from the course search widget
// with some static term ID\'s for each custom taxonomy.
$search_course_area = array(65, 62);
$search_course_level = array(117); //52 for further filtering
$search_course_mode = array(54, 56); 


//Begin a new query
$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\' => array(
                //Set the relation condition for the filters in 
                //this case we\'re using AND as it is explicity set 
                //by the user when searching
                \'relation\' => \'AND\',
                //First check retreive course-area\'s based on ID
                array(
                    \'taxonomy\' => \'course-area\',
                    \'field\' => \'id\',
                    \'terms\' => $search_course_area
                ),
                //And again for the second taxonomy
                array(
                    \'taxonomy\' => \'study-levels\',
                    \'field\' => \'id\',
                    \'terms\' => $search_course_level
                ),
                //Finally check retreive course-level\'s based on ID
                array(
                    \'taxonomy\' => \'course-mode\',
                    \'field\' => \'id\',
                    \'terms\' => $search_course_mode
                ),
        )
    )
);
我有点纠结的是,如果传递的数组是空的,这显然会破坏查询,并且不会返回任何结果。

解决这个问题最干净的方法是什么?我可以这样做:

if (isset($search_course_area)) {
    echo  "array(
                    \'taxonomy\' => \'course-area\',
                    \'field\' => \'id\',
                    \'terms\' => $search_course_area
                ),";
};
但我觉得这不是最好的方法?

非常感谢您的时间和任何帮助,我真的很感激!

2 个回复
最合适的回答,由SO网友:jessica 整理而成

您可以在WP_Query 实例化:

<?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,
    )
);
?>

SO网友:Wesley Andrade

以防万一,如果现在有人看到这一点,如果查询实例中有多个case,那么应该用另一个数组包装变量。。类似于:

$meta_query = array();

if ( 0 == $current_user->ID ) {
    $meta_query[] = array(array(
        \'key\'     => \'restrito_para_visualizar\',
        \'value\'   => \'1\',
        \'compare\' => \'!=\',
        \'type\'    => \'CHAR\',
    ),
    array(
        \'key\'     => \'ativo\',
        \'value\'   => \'1\',
        \'compare\' => \'=\',
        \'type\'    => \'NUMERIC\',
    ));
} else {
    $meta_query[] = array(
    array(
        \'key\'     => \'ativo\',
        \'value\'   => \'1\',
        \'compare\' => \'=\',
        \'type\'    => \'NUMERIC\',
    )); 
} 

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post