Get (and echo) Revision Date?
-
Hi,
I’m trying to manually set the order of several sticky posts in a home page ordered by date, changing the publication date solves that problem, but then you lose the canonical record of the actual original publication date.
Is there a simple way to access the revision history so I can put a / several particular revision dates (from the list of revisions I see on a post in the editor) in the post metadata, eg a “first published on <php the_revision_date(revision_id)>” or similar?
Thanks.
-
This topic was modified 11 months, 1 week ago by
mattspace.
-
This topic was modified 11 months, 1 week ago by
-
you can manually set a custom field first_published_date for each post and output it like this:
<?php echo get_post_meta( get_the_ID(), 'first_published_date', true ); ?>This avoids changing the canonical publish date and gives you full control.
-
This reply was modified 11 months, 1 week ago by
Ahmod Musa.
Could you please provide some more specificity for that? I add a new custom field to the post with:
name: first_published_date
value: do I need to enter the date in a specific format yyyy/mm/dd etc so that it matches whatever date style I’m using in the template to show the published date; eg:
<?php the_time('Y/m/d') ?>The reason I’m changing the published date is to make a particular post more recent than a subsequent one, so when they’re both sticky, the older post shows up above the newer one in a descending by date home page… so how does this date interact with that, or do I still change the post date to control the page order, and this is just to get the decorative originally posted date that I set with the custom field?
If you’re simply going to display the field, you can save it in any format you wish. If you want to do date calculations such as “published 10 months ago”, then I suggest saving in either YYYY-MM-DD HH:mm:ss format or as a Unix timestamp.
To answer your original question, you could query the posts table for posts whose post_parent field is the ID of the current post and whose post_status is “inherit” and post_type is “revision”. Order by date ascending to get the oldest revision first. Note that some systems are configured to only keep the n latest revisions, so this approach will not be reliable on all systems.
That said, AFAIK the post_date field never changes once published (post_modified field does change), unless someone manually alters it. Wouldn’t that be a reliable enough metric?
Bearing in mind I’m operating at the edge of my knowledge here…
If youβre simply going to display the field, you can save it in any format you wish.
OK, so having tested it, it’s literally just an arbitrary text string, rather than a date calculation call. That’s workable for me.
Is there a way I can make this happen so the text string “First Published:” doesn’t have to be in the custom field value itself?
e.g is there an addition that can be made to:
<?php echo get_post_meta( get_the_ID(), 'first_published_date', true ); ?>… which lets me put an echo of “First Published:” inside this, so it only shows if the first_published_date field has a recorded value?
<?php $first = get_post_meta( get_the_ID(), 'first_published_date', true ); if ( ! empty( $first )) echo "First published: $first"; ?>Oh thanks… so if I’m interpreting this correctly (I only have the faintest knowledge of programming, but I’m trying to learn)…
You’re starting by declaring a variable (that’s just an arbitrary name, not a special wordpress signifier?), whose content is filled by the php command to get the contents of the custom field, and then testing if the field’s value is NOT empty to echo a string “First Published:” followed by the contents of the variable. Is that correct?
That seems fairly comprehensible. Would it be better practice to have that implemented in the funchtions.php file (if you wouldn’t mind writing the code for that form of it), and just have a call to that function in my template file?
Thanks – that’s a solid new thing (setting an retrieving variables in php) I learned.
Yes, you fully understand my suggested code.
$firstis an arbitrary variable name. Use anything you like, as long as it’s used consistently. There are WP variables we should avoid using for other purposes, just to avoid confusion. They are all global variables, so as long as you do not declare a variable as global, there’s no chance of conflict as all variables by default are local (at least for PHP). So technically you could use WP variable names for other purposes as long as they remain local. I just feel that it’s confusing to do so.Managing what’s global and what’s local (or static) involves the concept of scope. If you’re keen to learn more about programming, I firmly believe there’s nothing like a real world programming need to help you learn. “Learn by doing” π Identify something simple you’d like to customize on your site, then figure out how to do it on your own. It’ll likely be very frustrating at first, but a lot is learned from failure. If you eventually feel rewarded when you arrive at the solution, then programming might be something you actually enjoy. Next try customizing something else, even if you end up undoing it in the end. The journey getting to a solution is more important than the end result.
Assuming you have a “classic” theme ( .php template files ), my suggested code is best used on an appropriate template file. In order to use it in functions.php, you’d need to identify an appropriate filter hook which let’s you alter specific content. By instead placing code in a template file, you can precisely control where the output occurs with little fuss.
If you instead have a block theme ( .html template files), you’d want to create a custom pattern, then reference the pattern on the appropriate template.
If your theme is subject to periodic updates, do not directly edit theme files. Updates will revert your changes. Instead, create a child theme to contain your customized templates.
For perspective, I’m coding my themes from scratch, as .php classic template files.
One thing I found worked well for me cognitively with the programming theory lessons I was doing (Swift Playgrounds), was creating custom functions so that instead of putting all the functional code inline at the point it’s used, you put all that in the non-running part of the file in discreet units of functionality, with an arbitrary name, and then at the place you want it to run, you just have the function name reference.
So the code I made ended up being effectively just a running list of references.
It made sense to me from a compartmentalisation perspective, and I’ve tended to do that with functions in my wordpress theme building, so I try to have:
<php? my_utility_function() ?>…and then all the actual stuff it does kept in the functions.php file. Buuut I know so little about how wordpress and php function that I really only learn things by seeing the completed example – I don’t have the knowledge base to build up, but I can backfill that knowledge by working backwards from the completed example (like the ones provided in this thread). Thats just how I learn if I’m not in a classroom with a live tutor.
So to my way of how I like to do things, I’d want to make the template file have something like this:
<!-- begin originally published date entry -->
<php? mytheme_get_the_original_date() ?>
<!-- end originally published date entry -->So, would this be close to the answer for functions.php:
function mytheme_get_the_original_date() {
$first = get_post_meta( get_the_ID(), 'first_published_date', true );
if ( ! empty( $first )) {
echo "First published: $first";
}
}Sure, that would be fine. There is always a quandary about whether code is better inline or as a function call. Personally, my criteria is “Can this function be reused elsewhere?”. If so, definitely a function call. Next in line, “Does using a separate function improve readability? (even if it’s for a one time usage)” If so, a function call is probably a good idea. However, if inline code would be informative about what’s going on and it’s reasonably brief, maybe a function call is not called for. As a general guideline, I always ask myself “When I or someone else needs to modify this code in a couple of years, will this all be easily understood?”
Ultimately, there is no right or wrong as long as it works as desired and doesn’t overload the server. It comes down to one’s sense of organization and readability. Beware though that it’s possible to get out into the weeds with hyper organization. When editing code, it’s often easier to see the inline code and not need to go hunting for some esoteric function that can only be found by grepping the entire code base.
A theme is really only a small part of a WP installation, there’s not much reason to get overly organized. We could go nuts with class objects and namespaces in the name of organization. These are great tools for larger products, but possibly over the top for a WP theme.
Disclaimer: I’m a self-taught coder (albeit with many years of experience in several languages), my perspective may not align with the consensus of formally educated professional programmers.
Oh so I was correct to put the echo into the curly braces for the functions.php part? If so, that was a bit of a conceptual leap for me, so I really appreciate your patience in answering my questions on this.
Thankfully, I’m the only person who needs to deal with my code or my themes, but I do need to deal with it every few years, and coming back to it a couple of years later, being able to just see:
<php? mytheme_get_the_original_date() ?>…is probably a little cleaner for me when reading it. I’m mostly editing my template files from a what-goes-where perspective, not a how-it-works one.
I find stuff being in functions.php is easier to deal with when building it up myself, but is harder to understand what a theme is doing, when trying to reverse-engineer it for customisation etc. That’s largely why I spent a couple of years on and off trying to build an entire new theme from scratch – so I’d know how every bit of it worked, and could document that in the comments.
Any organizational scheme that best helps you understand what’s going on is likely the best approach for you.
I reviewed your code for concept only, I wasn’t looking for any more subtle errors; however, I just noticed you have
<php?instead of<?php. The latter is correct, what you have is essentially a custom HTML tag and not a PHP demarcation π Probably just a typo here in the forums. If that were your actual code, it wouldn’t do anything, but it wouldn’t throw an error either.haha, nice catch, late night message replying, all my php code is
<?phpetc in my theme files.But yes, that’s my level of php fluency that I didn’t recognise it myself.
I managed to get it all working, after figuring out what could and could not sit within echo’d html. This isn’t throwing up any errors, and produces a wonderful conditional result where there’s nothing output on posts where I haven’t added the custom field:
function mytheme_get_the_original_date() {
$first = get_post_meta( get_the_ID(), 'first_published_date', true );
if ( ! empty( $first )) {
echo '<p class="entry-meta entry-meta-footer mod-date">
<span class="pre-in first-published">' , $first , '</span>
<span class="in">: Initial</span>
</p>';
}
}Assuming that’s all good and correct, thankyou so much for bearing with me on this little learning journey. It’s really been a significant improvement in my understanding of the system.
Assuming thatβs all good and correct,
Your assumption is correct π
You’re most welcome. Even if you were fluent in PHP beforehand, grasping how WP works is another learning experience on top of the usual learning a new programming language challenges. Happy coding!
Your assumption is correct π
Excellent, in that case I’ll mark the question as resolved. Much appreciated.
Cheers π
-
This reply was modified 11 months, 1 week ago by
The topic ‘Get (and echo) Revision Date?’ is closed to new replies.