[Support request] Updated post date included only when there's a revision

Home Forums Support Updated post date included only when there's a revision

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #998868
    Matthew

    Found this CSS

    .posted-on .updated {
        display: inline-block;
    }
    
    .posted-on .updated:before {
        content: "Last Updated: "
    }
    
    .posted-on .published:before {
        content: " | Published on: "
    }

    Adds an updated date to a post which is great:

    Last Updated: September 2, 2019 | Published on: September 2, 2019

    But it would be better if the part:

    Last Updated: September 2, 2019

    would only appear once the post has actually been revised. Currently it is shown for posts that have only been published but not had any additional revisions made. For those, I would like to have displayed:

    Published on: September 2, 2019

    thanks,

    #999063
    David
    Staff
    Customer Support

    Hi there,

    try this PHP Filter snippet:

    add_filter( 'generate_post_date_output', function( $output, $time_string ) {
    	$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on: %2$s</time>';
    
    	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
    		$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on: %2$s</time>
    		<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">| Last Updated: %4$s</time>';
    	}
    
    	$time_string = sprintf( $time_string,
    		esc_attr( get_the_date( 'c' ) ),
    		esc_html( get_the_date() ),
    		esc_attr( get_the_modified_date( 'c' ) ),
    		esc_html( get_the_modified_date() )
    	);
    
    	printf( '<span class="posted-on">%s</span> ', // WPCS: XSS ok, sanitization ok.
    		$time_string
    	);
    }, 10, 2 );  
    #1016409
    Matthew

    hi David,

    Before the snippet inserted I have:

    Last Updated: September 2, 2019 | Published on: September 2, 2019

    With the snippet inserted have:

    | Published on: Published on: September 2, 2019 | Last Updated: September 2, 2019

    I kept the css as is so far, but suspect I should remove the part:

    .posted-on .published:before {
        content: " | Published on: "
    }

    The post I’m testing on has been published and has no revisions. Any further ideas?

    thanks,

    #1016456
    David
    Staff
    Customer Support

    Hmmm – not seen this before but it looks like when a post is published WP is generating the UpdatedDate +1 minute.

    In this line:

    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {

    Try changing the two instances of 'U' to 'd-m-h'

    This will compare the Day / Month / Hour – it does mean that updates after to be older then an hour for it to display, which isn’t a bad thing.

    #1016536
    Matthew

    Runs like a clock now David

    #1016538
    David
    Staff
    Customer Support
    #1016819
    Matthew

    Hey just tested it again with a post that is saved as a draft first.

    In a different example test post I’ve had floating around a while as a draft and never published, the published date being used by snippet is the initial first Save of the draft. And the updated date is the actual real first publish date I just made:

    Published on: March 20, 2019 | Last Updated: September 23, 2019

    Can the snippet revised to disregard draft events do you reckon. I usually will save posts as a draft while I muck around composing them.

    thanks,

    #1016834
    David
    Staff
    Customer Support

    Are you using Gutenberg ?
    If so what happens if you use the Publish: Immediately?

    #1262527
    Yoeri

    Hello!

    I’m currently working with these snippets and something isn’t going right:

    I use the snippet above on a website for a few months now. It displays ‘Last update: november 26, 2019 by Pieter’

    But.. I’m now implenting it in a new website and I have the following:

    ‘april 29, 2020 Last update: april 29, 2020 by Dennis’

    What am I missing here why it shows different?

    Thank you guys! 🙂

    #1262786
    David
    Staff
    Customer Support

    Hi there,

    i think WP is being a pain in regards to how it captures date/times on new posts.
    Try the code provided here:

    https://generatepress.com/forums/topic/show-last-update-date-on-posts-and-google-results/page/2/#post-1257821

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.