WordPress.org

Ready to get started?Download WordPress

Forums

Paginating by year with date in custom field (1 post)

  1. highbulp
    Member
    Posted 8 months ago #

    I have inherited a WordPress based site and I'm trying to make some requested modifications, though I'm very new to the WordPress platform.

    I currently have a Custom Page Template for a custom post type ("Seminar"). Seminars have a field called _semdate that represents the date of the Seminar in Y-m-d fashion.

    The Custom Page Template originally listed the posts in chronological order by _semdate, paginated with 5 posts per page. What I would like to do is instead have the posts paginated by year, where the year is defined from this _semdate variable, not from the date the post was actually made; ideally, so that users could visit the paginated page as /seminar/page/2013 (for example).

    Based on this post)), I tried using keesiemeijer's date pagination plugin. I got the plugin to work, but it's paginating by the date the post was created, not the date in the _semdate field.

    Is it possible to use built-in pagination functionality (either from WordPress or a plugin) with the pagination based on the year extracted from a string in a custom field? Or will I need to somehow "roll my own" pagination code?

    What I expect are relevant parts of my Custom Page Template are below:

    <?php
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$args = array( 'post_type' => 'seminar',
    		'date_pagination_type' => 'yearly',
    		'paged'=> $paged,
    		'meta_key'=>'_semdate', 'orderby' => 'meta_value', 'order' => 'DESC' );
    	$loop = new WP_Query($args);
    ?>
    <?php if ($loop->have_posts()) : ?>
    	<h2><?php echo km_dp_get_current_date_label('Y', $loop); ?></h2>
    
    	<?php while ($loop->have_posts()) : $loop->the_post();
    		$sem_date = get_post_meta( get_the_ID(), '_semdate', true);
    
    		// more loop stuff
    
    	?>
    	<?php endwhile; ?>
    
    <!-- pagination -->
    // ... variable stuff
    <?php next_posts_link($next_label, $loop->max_num_pages) ?>
    <?php previous_posts_link($prev_label, $loop->max_num_pages) ?>

Reply

You must log in to post.

About this Topic