WordPress.org

Ready to get started?Download WordPress

Forums

Pagination by year? (16 posts)

  1. ikjosh
    Member
    Posted 10 months ago #

    Hi,

    I have created a custom template for a specific category. On that page I query the category and list out its posts. Right now it just lists 10 posts per page and uses the default WP pagination.

    However, I'd like to change it so that it lists posts by year. So default will list all posts in 2013. Then pagination will go back 2012, 2011, 2010, etc. and list all posts in those years.

    An added tricky part is it needs to sort and group the years not by published date, but by a custom field.

    Any ideas?

    Thanks!

  2. ikjosh
    Member
    Posted 10 months ago #

    Anyone? :)

  3. keesiemeijer
    moderator
    Posted 10 months ago #

    Is this a category template:
    http://codex.wordpress.org/Template_Hierarchy#Category_display

    or a custom page template:
    http://codex.wordpress.org/Page_Templates#Custom_Page_Template

    Do all the posts in the specific category have the custom field by which you want to order the posts?

  4. ikjosh
    Member
    Posted 10 months ago #

    Hi keesiemeijer,

    It is a custom page template, and yes they all use the same custom field for the date.

    Here is what I am using..

    <?php
    					$i = 1;
    					$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    					query_posts( array(
    									  'cat' => '17',
    									  'post__not_in' => array($post_ID),
    									  'order' => 'DESC',
    									  'orderby' => 'meta_value_num',
    									  'meta_key' => 'event_date',
    									  'posts_per_page' => '12',
    									  'paged' => $paged
    								 ) );
    				?>
    				<?php while ( have_posts() ) : the_post(); ?>
    				<?php
    					$get_event_date = get_post_meta($post->ID, 'event_date', true);
    					$get_event_date = substr($get_event_date,0,-3);
    					$event_date = date("m/d/Y", $get_event_date); 
    
    				?>
  5. keesiemeijer
    moderator
    Posted 10 months ago #

    I've just created a plugin that you can use. Download it here http://keesiemeijer.wordpress.com/date-pagination/

    Activate the plugin and try it with a custom query (WP_Query):

    <?php
    $i = 1;
    // not needed (plugin takes over pagination)
    //$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $args = array(
    	'cat' => '17',
    	'post__not_in' => array( $post_ID ),
    	'order' => 'DESC',
    	'orderby' => 'meta_value_num',
    	'meta_key' => 'event_date',
    	'date_pagination_type' => 'yearly',
    	//'posts_per_page' => '12', why show 12 posts?
    	//'paged' => $paged
    );
    
    // the custom query
    $the_query = new WP_Query( $args );
    
    // set the max_num_pages variable
    if ( function_exists( 'km_dp_set_max_num_pages' ) )
        km_dp_set_max_num_pages( $the_query );
    ?>
    				<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <?php
    $get_event_date = get_post_meta( $post->ID, 'event_date', true );
    $get_event_date = substr( $get_event_date, 0, -3 );
    $event_date = date( "m/d/Y", $get_event_date );
    ?>
  6. ikjosh
    Member
    Posted 10 months ago #

    Thanks keesiemeijer... I activated the plugin, used the code above, but all it's doing now is just listing all the posts on one page. Any ideas?

  7. keesiemeijer
    moderator
    Posted 10 months ago #

    I forgot some code when posting, does your code contain this (see edited example above):

    // set the max_num_pages variable
    if ( function_exists( 'km_dp_set_max_num_pages' ) )
        km_dp_set_max_num_pages( $the_query );
  8. ikjosh
    Member
    Posted 10 months ago #

    Yes it does

  9. keesiemeijer
    moderator
    Posted 10 months ago #

    Can you post the full template.

  10. ikjosh
    Member
    Posted 10 months ago #

    <?php
    
    get_header();
    ?>
    <!-- Body -->
    <div class="wrapper">
    	<div id="leftNav">
    		<h4>Menu</h4>
    		<?php wp_nav_menu( array( 'container_class' => '', 'menu' => 'Sidebar Menu', 'depth' => '1' ) ); ?>
        </div>
        <div id="centerCol">
        	<div id="centerBanner">
    		<?php
    			if ( has_post_thumbnail() ) {
    				the_post_thumbnail();
    			}else{
    
    			}
    		?>
    		</div>
            <div id="contentArea">
            	<div class="pad">
    			<div id="speakers">
    
    				<h2>Previous</h2>
    
    				<?php
    					$i = 1;
    					// not needed (plugin takes over pagination)
    					//$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    					$args = array(
    						'cat' => '17',
    						'post__not_in' => array( $post_ID ),
    						'order' => 'DESC',
    						'orderby' => 'meta_value_num',
    						'meta_key' => 'event_date',
    						'date_pagination_type' => 'yearly',
    						//'posts_per_page' => '12', why show 12 posts?
    						//'paged' => $paged
    					);
    
    					// the custom query
    					$the_query = new WP_Query( $args );
    
    					// set the max_num_pages variable
    					if ( function_exists( 'km_dp_set_max_num_pages' ) )
    						km_dp_set_max_num_pages( $the_query );
    				?>
    				<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    				<?php
    					$get_event_date = get_post_meta( $post->ID, 'event_date', true );
    					$get_event_date = substr( $get_event_date, 0, -3 );
    					$event_date = date( "m/d/Y", $get_event_date );
    				?>
    				<div id="<?php echo $i; ?>">
    					<div class="img"><?php the_post_thumbnail('speaker-thumbnail'); ?></div>
    					<div class="info">
    						<span class="title"><strong><?php the_title(); ?></strong></span><br />
    						<span class="date"><?php echo $event_date; ?> - Guest Speaker <?php echo get_post_meta($post->ID, 'Wise Talk Speaker', true) ?></span>
    						<a class="redbutton" href="<?php the_permalink(); ?>" style="margin-top: 10px;">listen</a>
    					</div>
    					<div class="break"></div>
    					<hr />
    				</div>
    				<?php $i++; endwhile; ?>
    				<div class="navigation">
    					<div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
    					<div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
    				</div>
    				<?php wp_reset_query(); ?>
    				<div class="break"></div>
    
    			  </div>
    		  </div>
            </div>
            <div id="rightSidebar">
            	<?php dynamic_sidebar(); ?>
            </div>
            <div class="break"></div>
        </div>
        <div class="break"></div>
    </div>
    <?php
    get_footer();
    ?>
  11. keesiemeijer
    moderator
    Posted 10 months ago #

    That't strange, just tested it with your code in a page template and it seems to work. So now it is listing all the posts on the page from any year?

    Where do you get the $post_ID variable from? Try changing it to $post->ID. Also change this:

    <div class="alignright"><?php next_posts_link('More »') ?></div>

    to this:

    <div class="alignright"><?php next_posts_link('More »', $the_query->max_num_pages) ?></div>

    http://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query

    Maybe try deactivating all plugins but this one to see if this resolves the problem? If this works, re-activate the plugins one by one until you find the problematic plugin(s).

  12. keesiemeijer
    moderator
    Posted 10 months ago #

    I've tested some more and it seems you have to set the $paged variable in a static front page. Is this the page template for a static front page? I've updated the plugin so it works also for a static front page. Download the plugin once more at the same location and try it with this:

    <?php
    $i = 1;
    
    // current page
    if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
    elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
    else { $paged = 1; }
    
    $args = array(
    	'cat' => '17',
    	'post__not_in' => array( $post->ID ),
    	'order' => 'DESC',
    	'orderby' => 'meta_value_num',
    	'meta_key' => 'event_date',
    	'date_pagination_type' => 'yearly',
    	'paged' => $paged,
    );
    
    // the custom query
    $the_query = new WP_Query( $args );
    
    // set the max_num_pages variable
    if ( function_exists( 'km_dp_set_max_num_pages' ) )
    	km_dp_set_max_num_pages( $the_query );
    ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <!-- rest of loop -->

    http://codex.wordpress.org/Pagination#static_front_page

  13. ikjosh
    Member
    Posted 10 months ago #

    Excellent, that seems to be working!

    Is there a way to alter the pagination so that it goes like this...

    2013 - 2012 - 2011 ... 2009

    Instead of Next > and Prev ?

  14. keesiemeijer
    moderator
    Posted 10 months ago #

    @ikjosh
    I added and removed some functions from the plugin.
    Remove this snippet from your template:

    // set the max_num_pages variable
    if ( function_exists( 'km_dp_set_max_num_pages' ) )
    	km_dp_set_max_num_pages( $the_query );

    It is no longer needed (and function removed) to get the correct number of pages for a custom query.

    Re-upload the new plugin and see the last example in the documentation on how you can add the year labels to pagination functions.

  15. onorok
    Member
    Posted 6 months ago #

    keesiemeijer,

    Is it possible to view the current year for the page you're on?

    I tried using km_dp_date_pagination_get_date('Y', $posts) but it didn't work for me, it returned the previous year.

    Any ideas?

    I greatly appreciate this plugin, it's exactly what I was needing!

  16. keesiemeijer
    moderator
    Posted 6 months ago #

    I've updated the plugin to also let you get the current year with the km_dp_get_current_date_label(); function:
    http://keesiemeijer.wordpress.com/date-pagination/#functions

    <?php
    echo 'Year: ' . km_dp_get_current_date_label('Y');
    ?>

    Get the new version 0.2 here:
    https://github.com/keesiemeijer/date-pagination/releases

Reply

You must log in to post.

About this Topic