Reputation: 269
I'm trying to get wordpress post by date and time, for example from 02/01/2014 to 02/05/2014 and time 17:10, somebody knows?
thanks for the help
Upvotes: 1
Views: 2441
Reputation: 24107
The WP_Query class can fulfill this. Just set up a new query, passing in the appropriate arguments:
$args = array(
'date_query' => array(
// limit to between these dates
array(
'after' => '2014-02-01',
'before' => '2014-02-05', //remove this line if no upper limit
'inclusive' => true,
),
// limit to posts before 17:10 (not tested)
array(
'hour' => 17,
'minute' => 10,
'compare' => '<=',
),
// limit to posts after 08:30
array(
'hour' => 08,
'minute' => 30,
'compare' => '>=',
),
),
'posts_per_page' => -1,
);
$my_date_query = new WP_Query( $args );
Then run your loop using the query object you just instatiated:
<?php if ( $my_date_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $my_date_query->have_posts() ) : $my_date_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
The date parameters section gives a good overview of the types of post queries you can create: http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
N.B Don't use query_posts
. From the codex:
query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances
Upvotes: 6