Wordpress: Add filter to query_posts to only show

2019-09-06 15:38发布

问题:

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