Bringing Back a Bit of the Past

From Ages Past

I’ve really been missing the of ‘Days of Ages Past‘ chunk of code that used to be on the page. The ‘related posts’ are fun… but there’s something about looking backwards in time that’s addicting. (Which is probably why Facebook and Google are chasing the same idea.)

But when I went to cut and paste in the code into single.php… it didn’t work.

And that means, it’s adventuring time! πŸ˜€

Starting from Not-Scratch

This is what the old version of the code looks like:

< ?php
  $GetDay = get_the_date( "d", $before, $after, FALSE );
$GetMonth = get_the_date( "m", $before, $after, FALSE );
 
  $GetID = get_the_ID();
 
  $string = "monthnum=".$GetMonth."&day=".$GetDay;
 
  echo <pre>  </pre>';
  echo '<blockquote><strong><h4>Posts From this Day in Ages Past</h4></strong>';
  echo '<ul>';
 
  $second_query = new WP_Query( $string);
 
  while( $second_query->have_posts() ) : $second_query->the_post();
 
    if ( get_the_ID() != $GetID ) :
      <li><a href="<?php if($title_url) echo $title_url; else the_permalink(); ?>" rel="bookmark" title="< ?php _e('Permanent Link:','mystique'); echo ' '; the_title_attribute(); ?>">< ?php the_title(); echo "  ("; echo get_the_date( "Y", $before, $after, FALSE ); echo ")"; ?></a></li>
    < ?php endif; 
  endwhile;
 
  wp_reset_postdata();
  echo '</ul></ul></blockquote>';  ?>

It’s not working and that’s probably because WordPress has changed quite a bit from 2011. So, let’s take a look at this and see where things have gone wrong…

Checking the Functions

get_the_date has changed since this was written. I could use the_date but (after testing) it will only work once so I can’t get both the month and the day.

get_the_ID doesn’t appear to have changed.

monthnum and day are still valid query terms for WP_Query so the search string should still be okay.

have_posts and the_permalink don’t seem to have changed at all.

_e(‘Permanent Link:’,’mystique’) is a holdover from the Mystique theme. I believe I can just take this out, but I’ll need to look at the HTML output…

the_title_attribute() and the_title() appear to do the same thing, so I think I can clean up line 17 a bunch. *pokes line*

wp_reset_postdata should still be okay as well.

Tweaking the Code

So using what I’ve learned above, I decided to try out this modified version. I only changed a few of the lines:

  $GetMonth = get_the_date("m");
  $GetDay = get_the_date("d");
<li><a href="<?php if($title_url) echo $title_url; else the_permalink(); ?>" rel="bookmark" title="< ?php echo the_title_attribute(); ?>">< ?php the_title(); echo "  ("; echo the_date( "Y", $before, $after, FALSE ); echo ")"; ?></a></li>

And of course it didn’t work at all.

I’m pretty sure it’s Line 17 that’s the issue, but in good troubleshooting fashion I started from scratch and slowly added in the pieces.

I started out by pulling the three variables and building the string to make sure I was getting what I expected. (And, this is messy messy formatting for the code, but it was just for testing.)

< ?php
  $GetDay = get_the_date("d");
  $GetMonth = get_the_date("m");
  $GetID = get_the_ID();
  $string = "monthnum=".$GetMonth."&day=".$GetDay;
 
  echo '<pre>  </pre>';
  echo '<strong><h3>Testing for - Posts From this Day in Ages Past</h3></strong>';
  echo '<ul>';
  echo '<li> Month ';
  echo $GetMonth;
  echo '</li>';
  echo '<li> Day ';
  echo $GetDay; 
  echo '</li>';
  echo '<li> ID ';
  echo $GetID;
  echo '</li>';
  echo '<li> Search string ';
  echo $string;
  echo '</li>';
  echo '</ul>';
  echo '<pre>  </pre>';
?>

This got me the following… so far so good! πŸ™‚


Testing for – Posts From this Day in Ages Past

  • Month 02
  • Day 04
  • ID 56812
  • Search string monthnum=02&day=04

So I know that I’ve got good variables, now it’s time to add in the rest of the code to see what might be causing the issue.

First I added in just the WP_Query pull, to see if it might be something wrong with the string I was passing in.

< ?php
  $GetDay = get_the_date("d");
  $GetMonth = get_the_date("m");
  $GetID = get_the_ID();
  $string = "monthnum=".$GetMonth."&day=".$GetDay;
 
  $second_query = new WP_Query( $string);
 
  while( $second_query->have_posts() ) : $second_query->the_post();
  endwhile;
?>

That pulled up the page just fine, so no errors on the query. So it’s probably the way I was trying to display the information I was returning from the query (aka Line 17!). So I pulled out all the fancy stuff and got back to just the basics…

echo '<strong><h3>Testing for - Posts From this Day in Ages Past</h3></strong>';
 echo '<ul>';
 
 $second_query = new WP_Query( $string);	
 while( $second_query->have_posts() ) : $second_query->the_post();
 if ( get_the_ID() != $GetID ) :	 
 echo '<li>';
 echo $GetID;	 	
 echo '';	 	
 echo the_title();	 
 echo '';	 
 echo the_date("Y");	 	
 echo '</li>';	 
 endif;	 
 endwhile;	 	
 echo '</ul>';	 	
 echo '<pre> </pre>';	
?>

That got me what I was looking for! Well, in a messy messy fashion…


Testing for – Posts From this Day in Ages Past


Now that I’d figured out how to pull (and display) the data, I trimmed out all of the debugging bits and reformatted things so was easier to read.

< ?php
  $GetDay = get_the_date("d");
  $GetMonth = get_the_date("m");
  $GetID = get_the_ID();
 
  $string = "monthnum=".$GetMonth."&day=".$GetDay;
 
  echo '<pre> </pre>';
  echo '<strong><h3>Posts From this Day in Ages Past</h3></strong>';
  echo '<pre> </pre>';
  echo '<ul>';
 
  $second_query = new WP_Query( $string);
 
  while( $second_query->have_posts() ) : $second_query->the_post();
    if ( get_the_ID() != $GetID ) :
      echo '<li>';
      echo the_date("Y");
      echo ' - ';
      echo the_title();
      echo '   </li>';
    endif;
  endwhile;
 
  echo '</ul>  <pre> </pre>';
  wp_reset_postdata();
?>

The formatting is still not perfect (I’d really like to get the colored box back), but the data being returned is what I wanted. So for now I’m happy… and for later I’ll have another post about tweaking the CSS! πŸ˜‰

Martha Bechtel

My name is Martha Bechtel and I write fantasy and science fiction stories, paint small model horses silly colors, cast resin and plaster magnets, code random code (and Wordpress plugins)... Come on in and join in the fun!

Leave a Reply