Wordpress: Add filter to query_posts to only show posts with “date” set to today or future

£可爱£侵袭症+ 提交于 2019-12-11 18:09:11

问题


I am using this query to display my posts:

$today = getdate();
$year=$today["year"];
$month=$today["mon"];
$day=$today["mday"];

query_posts( $query_string.'order=ASC' . '&post.status=future,publish'.'&year='.$year.'&monthnum='.$month );
?>

Now I want to add a filter to only show posts that have been published TODAY or will be published in the future.

Example: Today is the 22nd of March. PostA was published on the 1st of March, PostB has been published today and PostC will be published (status 'future' is already in my query, so it nevertheless will be displayed) on the 24th of March. My new filter for the query should only display PostB and PostC.

Thanks a lot in advance!

:-)


回答1:


Try this :

// Create a new filtering function that will add our where clause to the query
  function filter_where( $where = '' ) {
$today = date('Y-m-d');
  // posts for today and future
$where .= " AND post_date >= $today ";
return $where;
  }

  add_filter( 'posts_where', 'filter_where' );
  $query = new WP_Query( $query_string );
  remove_filter( 'posts_where', 'filter_where' );



回答2:


Try This:

$archive_year  = get_the_time('Y'); $archive_month = get_the_time('m'); $archive_day   = get_the_time('d'); query_posts('year=' .$archive_year .'&monthnum=' .$archive_month .'&day=' .$archive_day);


来源:https://stackoverflow.com/questions/15580600/wordpress-add-filter-to-query-posts-to-only-show-posts-with-date-set-to-today

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!