收集过去48小时内的帖子

时间:2012-01-09 作者:Mohammad Umer

I want to create a custom rss feed

<?php
/*
Template Name: Custom Feed
*/

function filter_where($where = \'\') {
    //posts in the last 30 days
    $where .= " AND post_date > \'" . date(\'Y-m-d\', strtotime(\'-2 days\')) . "\'";
    return $where;
  }
add_filter(\'posts_where\', \'filter_where\');
    $args=array(
      \'post_type\' => \'post\',
      \'post_status\' => \'publish\',
      \'showposts\' => -1,
      \'caller_get_posts\'=> 1
      );
$my_query=new WP_Query($args);
remove_filter(\'posts_where\', \'filter_where\');
header("Content-Type: application/rss+xml; charset=UTF-8");
echo \'<?xml version="1.0"?>\';
?><rss version="2.0">
<channel>
  <title>Something</title>
  <link>#</link>
  <description>The latest blog posts from #</description>
  <language>en-us</language>
<?
  if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
      <item>
    <title><?php the_title(); ?></title>
    <link><?php the_permalink() ?></link>
  </item>
     <?php
    endwhile;
  } //if ($my_query)?>
</channel>
</rss>
<?PHP wp_reset_query(); //just in case
?>

Now the final code for Custom Rss feed fetch from 48 hours. I can get right results from it.

Thanks for your help.

Mohammad Umer

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

从未对此进行过测试,但我认为您需要在查询中添加WHERE子句,并使用时差如下:

$timediff .= " AND post_date > \'" . date(\'Y-m-d\', strtotime(\'-2 days\')) . "\'";
你也可以在法典中找到它

http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters

如果不想在查询中使用,但在循环中使用,也可以使用

<?php while (have_posts()) : the_post(); ?>

<?php $mylimit=15 * 86400; //days * seconds per day
$post_age = date(\'U\') - get_post_time(\'U\');
if ($post_age < $mylimit) { ?>

// your functions or display here 

<?php } ?>
<?php endwhile; ?>

结束

相关推荐