WordPress.org

Ready to get started?Download WordPress

Forums

Link to paginated archive using a custom date field (6 posts)

  1. grabbagmedia
    Member
    Posted 1 year ago #

    I'm a web designer still learning my way through php, so I'd love some pointers on a unique challenge I have:

    I have a site with a custom post type for comic strips. I would like to make an archive page formatted like so:

    2003
    Description of story arc
    January February March

    Description of story arc
    April May

    Where each month is a link that takes you to that month's posts.

    Seems easy enough, but here's the kicker: the posts are organized not by post date, but by a custom field for the original date the art was made, which uses this format: yyyy-mm-dd-x , in which x is a number that helps sort the order for art made on the same day.

    So what kind of function can I make that will allow me to call from this page an archive page of just the posts matching the year and month parts of that custom field?

  2. vjpo
    Member
    Posted 1 year ago #

    Hello! I'd recommend to simplify solution. I mean to change post dates according to dates when drawings were made, then to use default WP feature that orders posts by date. Posts created on the same day can be arranged by time.

  3. grabbagmedia
    Member
    Posted 1 year ago #

    vjpo:

    That's not a bad idea. The only problem then is when I publish a new post, it wouldn't appear in my main blog feed (mixed post types) because the publish date wouldn't show it as being new.

    I wonder, if I changed the art date field from a string to an actual date field if it would be an easier fix...

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Try and create a custom page template for the months posts results:
    http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates

    On your archive page add a query argument to the custom page with the year and month:

    <?php
    // change 'date' custom field key to your custom field key
    $date = trim( (string) get_post_meta( $post->ID, 'date', true ) );
    if ( $date != '' ) {
    	// check if date is made of numbers and dashes
    	if ( !preg_match( '/[^0-9\-]/', $date ) ) {
    		// check if date has dashes
    		$pos = strpos( $date, '-' );
    		// check if date is formatted the way we want yyyy-mm-dd-x
    		if ( ( $pos !== false ) && ( ( $pos > 0 ) && ( $pos != ( strlen( $date )-1 ) ) ) ) {
    			// get year and month
    			list( $year, $month ) = explode( '-', $date );
    
    			// check if year and month is 6 characters long
    			if ( ( strlen( $year.$month ) == 6 ) ) {
    				// change custom page ID to your custom page page ID
    				$url = get_permalink( 1690 ); // custom page ID 1690
    				// add query arg date to custom page link
    				$url = add_query_arg( 'date', $year . $month, $url );
    				// output the month link
    				$month_name = date("F",mktime(0,0,0,$month,1,$year));
    				echo '<a href="'.esc_url( $url ).'">'.$month_name.'</a>';
    			}
    		}
    	}
    }
    ?>

    On your custom page template query for the custom field month posts like so:

    <?php
    $have_posts = false;
    if ( isset( $_GET['date'] ) && strlen( $_GET['date'] ) == 6 ) {
    
    	// check if date is a number
    	if ( !preg_match( '/[^0-9]/', $_GET['date'] ) ) {
    		$year = absint( substr( $_GET['date'], 0, 4 ) );
    		$month = zeroise( absint( substr( $_GET['date'], 4, 2 ) ), 2 );
    		$date_key = $year.'-'. $month;
    
    		// check if a similar meta_value exists
    		global $wpdb;
    		$query = "SELECT * FROM $wpdb->postmeta WHERE meta_key = 'date' AND meta_value LIKE %s ";
    		$results = $wpdb->get_results( $wpdb->prepare( $query, like_escape( $date_key ) . '-%' ) );
    
    		if ( $results ) {
    			$key= 'date';
    			$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    			$args = array(
    				'post_type'      => 'post',
    				'paged'          => $paged,
    				'posts_per_page' => 10,
    				'meta_key'       => $key,
    				'orderby'        => 'meta_value',
    				'order'          => 'ASC',
    				'meta_query'     => array(
    					array(
    						'key'     => $key,
    						'value'   => array( $year.'-'. $month . '-01-1', $year.'-'. $month . '-31-9999' ),
    						'compare' => 'BETWEEN',
    						'type'    => 'NUMERIC',
    					)
    				)
    			);
    			query_posts( $args );
    			if ( have_posts() )
    				$have_posts = true;
    		}
    	}
    }
    
    ?>
    <?php if ( $have_posts ) : ?>
    <!-- the loop -->
    <?php while ( have_posts() ): the_post(); ?>
    <!-- rest of loop -->
    <?php get_template_part( 'content', get_post_format() );  ?>
    <?php endwhile; ?>
    <?php else : ?>
    <p>No Posts Found</p>
    <?php endif; ?>
  5. grabbagmedia
    Member
    Posted 1 year ago #

    keesiemeijer:
    Your suggestion gave me a lot to think about, since I'm still learning. Thank you for that.

    In the end, and after a lot more research, I think the way to go is going to be to take the custom date field and copy it into a hierarchical taxonomy of years and months. This seems like it will make overall navigation and urls a lot easier to manage.

    Now I need to figure out how to have WordPress automatically populate a hierarchical taxonomy, but that's a question for it's own topic.

    Thanks again!

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    No problem. I hope you find a solution.

Topic Closed

This topic has been closed to new replies.

About this Topic