I want to create a page labeled "Today's News" which displays a simple bulleted list of all posts published on the current day and grab the current date based on the server's date/time. Is this feasible?
Thank you for any help.
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityIs this feasible?
Yes.
get_posts();
is your best friend. No need to modify the query or run a new one.
// Call it with the date you need as input argument
function wpse28632_get_daily_posts( $date = null )
{
$posts = get_posts( array( 'order_by' => 'post_date' ) ); // additional arguments - see Codex
echo '<ul>';
foreach ( $posts as $post )
{
if ( $date == $post->post_date )
echo "<li>{$post}</li>";
}
echo '</ul>';
}
get_posts();
is nothing else than a new WP_Query
. This has two advantages: 1) It's easier than doing a new query, saving the object
and picking the stuff you need and the args are partly shared with query, but just simplified. Imo array
is easier to understand for most than object
. 2) Less chance to make failures. If you don't save the result in some var, it will be performed, but nothing happens.