WordPress.org

Ready to get started?Download WordPress

Forums

Using Page Navigation with custom time based query (6 posts)

  1. giant slayer
    Member
    Posted 2 years ago #

    I am using a custom query to pull my most recent posts from the last 120 days. I cannot figure out how to get the navigation and paging to work with the query. My code is below.

    function filter_where( $where = '' ) {
    	// posts in the last 120 days
    	$where .= " AND post_date > '" . date('Y-m-d', strtotime('-120 days')) . "'";
    	return $where;
    }
    add_filter( 'posts_where', 'filter_where' );
    
    $query = new WP_Query( 'paged=' . get_query_var( 'page' ).'&posts_per_page=9&cat=6&orderby=rand' );
    remove_filter( 'posts_where', 'filter_where' );

    After the endwhile statement closing the loop, I have the following nav links which produce no navigation.

    <div class="nav-previous">
    <?php previous_posts_link( __( '<span class="meta-nav">←</span>Go Back', 'twentyeleven' ) ); ?>
    </div>
    
    <div class="nav-next">
    <?php next_posts_link( __( 'More in this Gallery<span class="meta-nav">→</span>', 'twentyeleven' ) ); ?>
    </div>

    Any idea how I might add the navigation and paging with my time based query?
    Thanks,
    --christophe

  2. giant slayer
    Member
    Posted 2 years ago #

    I appologize for the long code blocks. I am not sure why they did that.

  3. giant slayer
    Member
    Posted 2 years ago #

    I found that the archive links do what I want as far as pulling posts from a selected date range and then having the paging and navigation links working correctly.

    Does anyone know how to implement this same thing for a custom date range?

    Thanks,
    --christopher

  4. giant slayer
    Member
    Posted 2 years ago #

    The codex says for pagination to work, one should set get_query_var( 'page');

    Pagination Note: You should set get_query_var( 'page' ); if you want your query to work with pagination. Since WordPress 3.0.2, you do get_query_var( 'page' ) instead of get_query_var( 'paged' ). The pagination parameter 'paged' for WP_Query() remains the same.

    How do I go about doing this?

  5. giant slayer
    Member
    Posted 2 years ago #

    This is very discourging. I cannot get the page navigation to work with anything I have tried.

    I have tried
    $page = (get_query_var('page')) ? get_query_var('page') : 1;
    and
    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    and
    $wp_query->set('page','1');
    I have put paged argument within the query
    query_posts( array('paged'=> $page ,'posts_per_page'=>9,'cat'=>6,'orderby'=>'rand' ) );
    All of these set the page number to be 1. They create the nav link to go to the next page which is {http://theviolent.net/recent-works/page/2/} But when the link is clicked, it returns a page not found error.

  6. chryaner
    Member
    Posted 1 year ago #

    Hey Christophe

    Did you manage to fix this problem?

    Iliya

Topic Closed

This topic has been closed to new replies.

About this Topic