如何使用QUERY_POSTS函数正确获取去年的所有帖子?

时间:2011-02-04 作者:Ryan Lang

具体来说,我正在制作一个自定义rss提要。我根据wp includes文件夹中的rss2提要对其进行了建模。我可以使用标准的query\\u posts(\'posts\\u per\\u page=-1\')成功检索所有帖子。

以下是我如何尝试获取过去一年发布的内容:

$today = date(\'Y-m-d\');
$newdate = strtotime ( \'-1 year\' , strtotime ( $today ) ) ;
$ayearago = date( \'Y-m-d\' , $newdate );

function filter_where($where = \'\') {
        $where .= " AND post_date >= \'" . $ayearago . "\'";
    return $where;
}

add_filter(\'posts_where\', \'filter_where\');

query_posts($query_string);
是的,这只返回2011年以后的帖子。有人能帮我找出原因吗?

下面是我的自定义提要页面feed postlist中的主要源代码。php:

    /**
 * Custom RSS Feed
 *
 * @package WordPress
 */

header(\'Content-Type: \' . feed_content_type(\'rss-http\') . \'; charset=\' . get_option(\'blog_charset\'), true);
$more = 1;

echo \'<?xml version="1.0" encoding="\'.get_option(\'blog_charset\').\'"?\'.\'>\'; ?>

<rss version="2.0"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    <?php do_action(\'rss2_ns\'); ?>
>
<?php
$today = date(\'Y-m-d\');
$newdate = strtotime ( \'-1 year\' , strtotime ( $today ) ) ;
$ayearago = date( \'Y-m-d\' , $newdate );

function filter_where($where = \'\') {
        $where .= " AND post_date >= DATE(\'" . $ayearago . "\')";
    return $where;
}

add_filter(\'posts_where\', \'filter_where\');

query_posts(\'posts_per_page=-1\');
?>
<channel>
    <title><?php bloginfo_rss(\'name\'); wp_title_rss(); ?></title>
    <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
    <link><?php bloginfo_rss(\'url\') ?></link>
    <description><?php bloginfo_rss("description") ?></description>
    <lastBuildDate><?php echo mysql2date(\'D, d M Y H:i:s +0000\', get_lastpostmodified(\'GMT\'), false); ?></lastBuildDate>
    <language><?php echo get_option(\'rss_language\'); ?></language>
    <sy:updatePeriod><?php echo apply_filters( \'rss_update_period\', \'hourly\' ); ?></sy:updatePeriod>
    <sy:updateFrequency><?php echo apply_filters( \'rss_update_frequency\', \'1\' ); ?></sy:updateFrequency>
    <?php do_action(\'rss2_head\'); ?>
    <?php while( have_posts()) : the_post(); ?>
    <item>
谢谢你,

瑞安

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

你好@Ryan Long:

您有两个问题:

您需要包装date( \'Y-m-d\' , $newdate ); 在SQL中DATE() 函数,以及您定义的$ayearago 外面filter_where() 函数,因此它不在函数的范围内。您需要移动设置$ayearago 进入filter_where() 功能如下:

function yoursite_filter_where($where = \'\') {
  $today = date(\'Y-m-d\');
  $newdate = strtotime ( \'-1 year\' , strtotime ( $today ) ) ;
  $ayearago = date( \'Y-m-d\' , $newdate );
  $where .= " AND post_date >= DATE(\'{$ayearago}\')";
  return $where;
}
如果对你有用,请告诉我。。。

SO网友:zeo

有很多方法可以做到这一点:

$today = getdate();
$lastyear = $today[ \'year\' ] - 1;

query_posts( array(
        \'posts_per_page\' => -1,
        \'year\' => $lastyear,
    ) );

function your_function() {
    $today = getdate();
    $lastyear = $today[\'year\'] - 1;
    set_query_var( \'year\', $lastyear );
}

add_filter( \'pre_get_posts\', \'your_function\' );
也用于feed,可能需要添加is_feed()

结束

相关推荐