WP 3.1让Tax_Query在Query_Posts()中工作

时间:2011-02-28 作者:Osu

我正试图使用以下代码对我的自定义帖子使用多个自定义分类法进行筛选,但我的新代码一直是空的,即循环中没有帖子出现。

其工作原理如下:用户从表单中的三个不同下拉列表中选择自定义分类法的术语“fttype”、“ftperiod”和“ftduration”,并将其传递给以下代码:

我最初有这段代码(虽然没有分页),但我正在尝试实现的新代码无法使用WP 3.1内置的通过多个自定义分类进行过滤的功能(请参阅下面的新代码)。

有人知道我做错了什么吗?这件事已经有一段时间了。。。

谢谢

osu

OLD CODE

// Set todays date to check against the custom field StartEventDate
$todaysDate = date(\'Y/m/d\');

// Convert spaces in taxonomies and terms into hyphens so that search works correctly (uses slug)
$ft_t_ns = osu_convert_spaces($ft_t);
$ft_p_ns = osu_convert_spaces($ft_p);
$ft_d_ns = osu_convert_spaces($ft_d);

// Build query
// NOTE: AS OF WP 3.1, SEE V2 FOR HOW TO PASS AN ARRAY TO query_posts(). YOU PROBABLY WON\'T NEED
// QUERY MULTIPLE TAXONOMIES PLUGIN EITHER FOR V2\'S APPROACH OF PASSING AN ARRAY TO WP_Query() TO WORK.
// READ MORE ON \'MULTIPLE TAXONOMY HANDLING\' HERE:
// http://codex.wordpress.org/Function_Reference/query_posts#Taxonomy_Parameters
$ft_args = \'post_type=ftevent\';
$ft_args .= \'&fttype=\' . $ft_t_ns;
$ft_args .= \'&ftperiod=\' .$ft_p_ns;
$ft_args .= \'&ftduration=\' . $ft_d_ns;
$ft_args .= \'&posts_per_page=\' . $ft_ppp;
$ft_args .= \'&meta_key=StartEventDate&meta_compare=>=&meta_value=\' . $todaysDate;
$ft_args .= \'&orderby=meta_value&order=ASC&paged=\' . $paged;

// Create query
query_posts($ft_args);

NEW CODE

$ft_args = array(
    \'post_type\' => \'ftevent\',
    \'posts_per_page\' => 5,
    \'paged\' => $paged,
    \'orderby\' => \'meta_value\',
    \'order\' => \'ASC\',
    \'meta_query\' => array(
        array(
            \'key\' => \'StartEventDate\',
            \'value\' => $todaysDate,
            // Custom field type. Possible values are \'NUMERIC\', \'BINARY\', \'CHAR\', \'DATE\', \'DATETIME\', \'DECIMAL\', 
            // \'SIGNED\', \'TIME\', \'UNSIGNED\'. Default value is \'CHAR\'.
            \'type\' => \'DATE\',
            // Operator to test. Possible values are \'LIKE\', \'NOT LIKE\', \'IN\', \'NOT IN\', \'BETWEEN\', \'NOT BETWEEN\'.
            // We choose \'BETWEEN\' because we need to know the date has not passed to show the event
            \'compare\' => \'BETWEEN\'
        )
    ),
    \'tax_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'taxonomy\' => \'fttype\',
            \'field\' => \'slug\',
            \'terms\' => $ft_t_ns,
            // Operator to test. Possible values are \'LIKE\', \'NOT LIKE\', \'IN\', \'NOT IN\', \'BETWEEN\', \'NOT BETWEEN\'.
            // We choose \'IN\' because we need to make sure the term is in the current array of posts
            \'operator\' => \'IN\',
        ),
        array(
            \'taxonomy\' => \'ftperiod\',
            \'field\' => \'slug\',
            \'terms\' => $ft_p_ns,
            \'operator\' => \'IN\',
        ),
        array(
            \'taxonomy\' => \'ftduration\',
            \'field\' => \'slug\',
            \'terms\' => $ft_d_ns,
            \'operator\' => \'IN\',
        ),
    )
);

// Create query
query_posts($ft_args);

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

$todaysDate 需要是两个值的数组BETWEEN 用于比较。

例如,如果您想使用从今天到未来一周的日期范围,那么您可以使用类似。。

$todaysDate = array(
    date(\'Y/m/d\'),
    date(\'Y/m/d\', strtotime(\'+1 week\') )
);
或者如果你想回到过去,那么也许。。

$todaysDate = array(
    date(\'Y/m/d\'),
    date(\'Y/m/d\', strtotime(\'-1 week\') )
);
Following on from the comments: 尝试将此作为meta_query 代码的一部分

\'meta_query\' => array(
    array(
        \'key\' => \'StartEventDate\',
        \'value\' => date(\'Y/m/d\'),
        \'type\' => \'DATE\',
        \'compare\' => \'>=\' // more than or equals
    )
)
NOTE: 仍然可以使用旧的>(超过),<(小于)和与新meta_query args,或者至少通过直接查看源代码看起来是这样的。

http://core.trac.wordpress.org/browser/trunk/wp-includes/meta.php#L355

希望这有帮助。

结束

相关推荐