Date of last modication instead of date of first publication

Hi there! Can't find (any other question on) how to change the date of posts for the last modification date instead of the date of first publication. I think it's a general wordpress question...
You use it: "last modified on...", I think it's really better!

Thank you

  • Nastia

    Hello v-i-n-c-e-n-t , I hope you are doing well!

    I think this is more related to a theme you are using than to General WordPress question. Please let me know what theme you are using?

    You will need to create a child theme and modify the functions.php file.

    Unfortunately, I am not a code expert here, but I can say that you will need to replace the the_date_xml(); function with the the_modified_date(); .

    Please see the bellow links fo reference:
    https://codex.wordpress.org/Function_Reference/the_modified_date
    https://codex.wordpress.org/Function_Reference/the_date_xml

    I hope this will help!

    Cheers,
    Nastia

  • v-i-n-c-e-n-t

    Hi Nastia, thank you for your response,
    I'm fine, and you?

    Well, I think it should be useful for several sites I'm working on, but, first of all, on one where I'm using the (free version of the) theme Blask.

    Hmmm, Ok, I understand that, if not already planned as an option in the theme, it's time for me to go further than just rewriting a few CSS lines and create my first child theme!! :grinning:
    And so, I've got to achieve some steps first!

    Thank you for the links :slight_smile:
    Cheers

  • v-i-n-c-e-n-t

    Hey, to be able to start, I found this 2 little plugins:
    The first works, but only adds a line at the beginning of the post, and with no customization possible:
    https://wordpress.org/plugins/post-updated-date/

    You can show the result on my website (much less cool than on wpmudev posts):
    http://v-i-n-c-e-n-t.com/sculpter-la-photographie/

    For the second one, well, 'don't know why, it refused to work with me:
    https://wordpress.org/plugins/wp-open-last-modified/

  • Sajid

    Hello v-i-n-c-e-n-t,
    Hope you are doing good today :slight_smile:

    Luckily the developer of Post Updated Date plugin used the filter to add the updated date in posts that we can remove and add our own by using the following code snippet. Add this code in functions.php file of your child theme or use it as mu-plugin.

    function wpmudev_filter_updated_post(){
    
    	remove_filter('the_content', 'wpt_updated_date');
    
    	add_filter('the_content', 'wpmudev_wpt_updated_date');
    
    }
    add_filter('init', 'wpmudev_filter_updated_post', 10);
    
    function wpmudev_wpt_updated_date($content){
    
    	$wpt_updated_date= '<span style="font-style:italic; font-weight:bold;text-align:center;">Add Whatever you want here by replace this text including brackets([post_updated])</span>';
    
    	if(is_single() && !is_home()) {
    	$content = $wpt_updated_date.$content;
    	}
    	return $content;
    }

    If you go with mu-plugin route then, go to your /wp-content/ folder via FTP, and find the folder called "mu-plugins". If there is no folder with that name, then you'll want to create one. Then, create a file inside that folder, and give the file any name you like and paste the code in there.

    You don't need to activate that plugin, as it will be always be automatically activated. Please be sure to add a PHP start tag at the beginning of the code if you insert this code as a mu-plugin, like this:

    <?php

    Feel free to edit the text in following line in above code according to your requirements.
    $wpt_updated_date= '<span style="font-style:italic; font-weight:bold;text-align:center;">Add Whatever you want here by replace this text including brackets([post_updated])</span>';

    Hope that helps! Feel free to post a reply if you need further assistance :slight_smile:

    Best Regards,
    Sajid

  • v-i-n-c-e-n-t

    Sajid, additionnaly, it should be great to automatically reorder posts in the front end by last updated in first. I only found articles for doing it manually. Is this the same kind of question or do you encourage me to open another ticket for that?
    (I found a nice litlle plugin for that: https://wordpress.org/plugins/wp-order-by/ last updated 7 months ago and unfortunately, not working...)

  • Sajid

    Hello v-i-n-c-e-n-t,
    Hope you are doing good today :slight_smile:

    The code provided in my last reply was suppose to use with Post Updated Date plugin.

    However, upon your request I changed it to work alone (delete that plugin :joy:slight_smile: and also I updated the code further to display post by last modified instead of creation date.

    // Get the last modified date and append before content on post single page.
    function wpmudev_wpt_updated_date($content){
    
    	// get the last mofidied date and return in form set in Settings ->
    	$the_time = get_post_modified_time( get_option('date_format'), null, null, true );
    
    	$wpt_updated_date= '<span style="font-style:italic; font-weight:bold;text-align:center;">Add Whatever you want here by replace this text including brackets('.$the_time.')</span>';
    
    	if(is_single() && !is_home()) {
    		$content = $wpt_updated_date.$content;
    	}
    	return $content;
    }
    add_filter('the_content', 'wpmudev_wpt_updated_date');
    
    // Belwo function will modify the defailt wp_query on frontend and change the order by to modified date instead of default chronological order.
    function exclude_single_posts_home($query) {
      if ( $query->is_main_query() and !is_admin() ) {
        $query->set('orderby', 'modified' );
      }
    }
    
    add_action('pre_get_posts', 'exclude_single_posts_home');

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid

  • v-i-n-c-e-n-t

    Hey Sajid!
    That's it! I've created my first child theme, added your code in functions.php, changed a few the css and the text before the date, as you said, uninstalled the precedent plugin, and ...it works!!
    Big thanks! :grinning:
    My regret is that you can see the updade date only if you open the post. What to do if I want to see it directly in the blog page, in the description of each post, directly under (or before) the first published date?

    Bests,

  • Sajid

    Hello v-i-n-c-e-n-t,
    Hope you are doing good today :slight_smile:

    Try this code instead to display on blog page too. This will still append the modified date before the content instead of before, after published date because that requires direct editing of the templates.

    // Get the last modified date and append before content on post single page.
    function wpmudev_wpt_updated_date($content){
    
    	// get the last mofidied date and return in form set in Settings ->
    	$the_time = get_post_modified_time( get_option('date_format'), null, null, true );
    
    	$wpt_updated_date= '<span style="font-style:italic; font-weight:bold;text-align:center;">Add Whatever you want here by replace this text including brackets('.$the_time.')</span>';
    
    	$post_type = get_post_type();
    
    	if( 'post' == $post_type && !is_admin()) {
    		$content = $wpt_updated_date.$content;
    	}
    	return $content;
    }
    add_filter('the_content', 'wpmudev_wpt_updated_date');
    add_filter('the_excerpt', 'wpmudev_wpt_updated_date');

    Additionally, as I would like to begin to understand php, I'd like to ask you about the name of the function, for instance in the line:
    `function wpmudev_wpt_updated_date($content){'
    is it any name you want, or this one precisely (i.e.: does it refer to another code, elsewhere)?

    Yes, this could be any name you want.

    However the filter or action hook name must match and should be relevant to execute your code at right time and place.

    See below article for more details about WordPress actions and filter hooks.
    http://docs.presscustomizr.com/article/26-wordpress-actions-filters-and-hooks-a-guide-for-non-developers

    This one, in case you really want to get into the coding:
    https://code.tutsplus.com/articles/the-beginners-guide-to-wordpress-actions-and-filters--wp-27373

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid

  • v-i-n-c-e-n-t

    It works too! Thank you Sajid! And the 2 links seems very useful too!

    (I used the new code instead of the first one but let the second part for re-ordering posts order, is that right?)

    What I don't understand is that I changed the css, and it works, but I tried to add a margin after the line, and, for that, nothing occurs. Here is the complete line of code:
    $wpt_updated_date= '<span style="font-style:italic; font-weight:400;text-align:left; font-size:13px; margin-bottom:16px;">Dernière mise à jour le '.$the_time.'</span>';
    ?

    And, for my second question, I understand that php is always esoteric to me, but gives results with a very few number of code's lines! :wink:

    Thanks again and take care too
    Bests
    Vincent

  • Sajid

    Hello v-i-n-c-e-n-t,
    Hope you are doing good today :slight_smile:

    Yes, you have to keep the second part of the code provided above for re-ordering. Here is the final code to be used for it.

    // Get the last modified date and append before content on post single page.
    function wpmudev_wpt_updated_date($content){
    
    	// get the last mofidied date and return in form set in Settings ->
    	$the_time = get_post_modified_time( get_option('date_format'), null, null, true );
    
    	$wpt_updated_date= '<span style="font-style:italic; font-weight:bold;text-align:center;">Add Whatever you want here by replace this text including brackets('.$the_time.')</span>';
    
    	$post_type = get_post_type();
    
    	if( 'post' == $post_type && !is_admin()) {
    		$content = $wpt_updated_date.$content;
    	}
    	return $content;
    }
    add_filter('the_content', 'wpmudev_wpt_updated_date');
    add_filter('the_excerpt', 'wpmudev_wpt_updated_date');
    
    // Belwo function will modify the defailt wp_query on frontend and change the order by to modified date instead of default chronological order.
    function exclude_single_posts_home($query) {
      if ( $query->is_main_query() and !is_admin() ) {
        $query->set('orderby', 'modified' );
      }
    }
    
    add_action('pre_get_posts', 'exclude_single_posts_home');

    For better management of tickets, please create separate ticket for styling of the date here. This will help us manage easily as well as helps other members of community to find the relevant answer without digging into whole thread.

    Also, when you start a new thread, don't forget to share the link of post where you need changes. This will help staff answer quickly.

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.