WordPress.org

Ready to get started?Download WordPress

Forums

How to use WP_Query to get posts from date archive page (3 posts)

  1. fooentes
    Member
    Posted 9 months ago #

    So, I'm using this in category.php

    $query = new WP_Query( array('cat' => $cat, 'posts_per_page' => 1, 'offset' => 3 ) );

    this in search.php

    $query = new WP_Query( array('s' => get_search_query(), 'posts_per_page' => 1, 'offset' => 3 ) );

    But I cannot find a way to do the same thing inside my archive.php, which I'm going to use only for dates.

    For example, when you visit mysite.com/?m=201301 I need to query only the posts from this date range.

    Is that possible?
    Thanks!

  2. fooentes
    Member
    Posted 9 months ago #

    anyone???

  3. Scott (@scottsweb)
    Member
    Posted 9 months ago #

    Ideally you would use the pre_get_posts hook (http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts) to make these changes. For example:

    function change_query( $query ) {
      if ( !is_admin() && $query->is_main_query() ) {
        if ($query->is_date()) {
          $query->set('posts_per_page', 5);
        }
      }
    }
    add_action( 'pre_get_posts', 'change_query' );

Reply

You must log in to post.

About this Topic