constantine
constantine

Reputation: 1

Selecting posts in Wordpress

I'm new to wordress and php. I need to display all posts from june 2009 to june 2010. How can I do that by creating a custom loop?

Upvotes: 0

Views: 60

Answers (2)

Using WP Query Time Parameters

year (int) - 4 digit year (e.g. 2011). monthnum (int) - Month number (from 1 to 12). w (int) - Week of the year (from 0 to 53). Uses the MySQL WEEK command. The mode is dependent on the "start_of_week" option. day (int) - Day of the month (from 1 to 31). hour (int) - Hour (from 0 to 23). minute (int) - Minute (from 0 to 60). second (int) - Second (0 to 60). m (int) - YearMonth (For e.g.: 201307).

Returns posts for just the current date:

   $today = getdate();
   $query = new WP_Query( 'year=' . $today["year"] . '&monthnum=' . $today["mon"] . '&day=' . $today["mday"] );

Returns posts for just the current week:

   $week = date('W');
    $year = date('Y');
    $query = new WP_Query( 'year=' . $year . '&w=' . $week );

Return posts for March 1 to March 15, 2010:

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts for March 1 to March 15, 2010
$where .= " AND post_date >= '2010-03-01' AND post_date < '2010-03-16'";
return $where;
 }

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

Return posts 30 to 60 days old

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts  30 to 60 days old
$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . "    AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
 }

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

For more reference click here http://codex.wordpress.org/Class_Reference/WP_Query

Upvotes: 1

WillxD
WillxD

Reputation: 200

query_posts() just allow to show posts from a specific week or month. However, you can show posts between two dates, adding a few lines of code. You need to paste this code wherever in your theme you'd like to display.

<?php
      function filter_where($where = '') {
            $where .= " AND post_date >= '2009-06-01' AND post_date <= '2010-06-30'";
        return $where;
      }
        add_filter('posts_where', 'filter_where');
        query_posts($query_string);
        while (have_posts()) :
          the_post();
          the_content();
        endwhile;
    ?>

Source: http://bit.ly/i5zXP0

Upvotes: 3

Related Questions