How to Display Posts by Modification Date in WordPress

How to Display Posts by Modification Date in WordPress

Most blogs contain static articles, that is, articles that do not evolve after their publication. The comments section is the only dynamic portion of those articles. It’s why archives are generally sorted by chronological order. It’s also why extensions such as Recent Comments exist: to highlight any new content on specific posts.

However, if your site specializes in articles requiring frequent updates, such as breaking news, it’s best to sort the archive by modification date.

Although WordPress does not provide this feature by default, it can be done using a plugin or by modifying your theme.

Method #1: Creating a WordPress Filter

If you have some PHP knowledge, you can modify the theme of your site. Be aware, however, that if you edit a theme that is automatically updated, you may lose those changes at some point. If this is your case, then you must create a child theme.

It’s necessary to modify the WP_Query loop parameters to change the order of the articles. To do this, we’ll create a WordPress filter. Then, we’ll apply this filter to the archive pages.

As a first step, add the following code to the functions.php file to create the filter:

function posts_by_modification_date($query) {
  if(is_archive() || is_home()) {
    $query->set('orderby', 'modified');
  }
}

Enable the filter by adding the next line to the functions.php file or at the beginning of the archive.php file:

<?php
add_filter('pre_get_posts', 'posts_by_modification_date');
?>

This method offers a lot of flexibility because it’s possible to specify more parameters. Here are all the ways you can order your posts:

  • ID: Sort articles by numeric ID.
  • author: Sort articles by author name.
  • title: Sort the entries in alphabetical order of the title.
  • name: Sort items by permalinks (slug).
  • date: Sort items by date (default).
  • modified: Sort the articles by modification date.
  • comment_count: Sort articles by the number of comments.

Besides, it’s possible to modify the sort order (ascending or descending) using the order parameter:

function archive_par_date_modification($query) {
  if(is_archive() || is_home()) {
    $query->set('orderby', 'modified');
    $query->set('order', 'ASC');
  }
}

The complete list of sorting parameters is here.

Method #2: Creating a PHP Function

If you want to display a list of items elsewhere in your theme, you need to create a custom function. It’s the case if you’re going to show a list of posts in the sidebar for instance.

Insert the following code into functions.php :

function recentPosts($orderby = 'modified', $posts_per_page = 5) {
  $args = array(
    'orderby' => $orderby,
    'ignore_sticky_posts' => '1',
    'posts_per_page' => $posts_per_page
  );

  $modified_posts_query = new WP_Query($args);

  if($modified_posts_query->have_posts()) {

    echo '<ul>';

    while($modified_posts_query->have_posts()) {
      $modified_posts_query->the_post();

      echo '<li>';
      echo '<a href="'.get_permalink($modified_posts_query->post->ID).'">';
      echo get_the_title($modified_posts_query->post->ID);
      echo '</a>';
      echo '</li>';
    }

    echo '</ul>';
  }

  wp_reset_postdata();
}

It’s imperative to call “wp_reset_postdata()” after the loop. This function tells WordPress that the use of the custom query loop is complete.

Next, insert this code at the desired location in your theme. It can be in a sidebar or the footer for example.

<?php
recentPosts();
?>

It’s also possible to specify the sort order and the number of items to display with this same function. For instance, you could use this code to list ten posts ordered by publication date:

<?php
recentPosts('date',10);
?>

With a little creativity, it is easy to add other items to the list, such as the number of comments and thumbnails.

Method #3: Using Shortcode

To include a list of articles in a WordPress page, you must create a “shortcode.” In WordPress, a “shortcode” is a line of code inserted into a post or page. WordPress replaces this shortcode with the result of a PHP function when generating the web page.

The first step is to create a function that generates a list of items. Insert the following code into the functions.php file:

function recentPosts($atts) {

  if(isset($atts['orderby'])) {
    $orderby = $atts['orderby'];
  } else {
    $orderby = 'modified';
  }

  if(isset($atts['posts_per_page'])) {
    $posts_per_page = $atts['posts_per_page'];
  } else {
    $posts_per_page = 5;
  }
  $args = array(
    'orderby' => $orderby,
    'ignore_sticky_posts' => '1',
    'posts_per_page' => $posts_per_page
  );

  $modified_posts_query = new WP_Query($args);

  if($modified_posts_query->have_posts()) {

    echo '<ul>';

    while($modified_posts_query->have_posts()) {
      $modified_posts_query->the_post();

      echo '<li>';
      echo '<a href="'.get_permalink($modified_posts_query->post->ID).'">';
      echo get_the_title($modified_posts_query->post->ID);
      echo '</a>';
      echo '</li>';
    }

    echo '</ul>';
  }

  wp_reset_postdata();
}

Next, enable the shortcode by adding this line to functions.php:

add_shortcode('recent_posts', 'recentPosts');

Finally, invoke the list of posts by inserting the following shortcode into a WordPress page:

[recent_posts orderby='modified' posts_per_page='10']

In this example, the shortcode accepts two parameters: orderby and posts_per_page. The parameter values are sent to the recentPosts function through the $atts array. It’s therefore possible to collect other shortcode parameters using $atts[‘name_of_parameter’] if necessary.

Method #4: Using a Plugin

If PHP coding is not your thing, you can use a plugin to sort and display posts. From the WordPress Dashboard, click Plugins > Add.

Perform a search for “display posts.” Install and activate the following plugin:

wordpress display posts plugin

There is no configuration to do once the plugin is active. To use it, insert a shortcode in a post or a page:

[display-posts orderby='modified']

By default, this plugin displays a list of entries as follows:

display posts plugin recent entries

However, it’s also possible to display the list like a table or grid using the “wrapper” parameter:

[display-posts image_size="medium" wrapper="div" wrapper_class="recent-entries grid" meta_key="_thumbnail_id" include_excerpt="true"]

This way, articles are grouped under “<div>” tags rather than “<li>” tags. However, it’s up to you to create the CSS code to layout the grid.

Insert the following code into the style.css file of your theme:

.recent-entries.grid,
.recent-entries.grid .listing-item {
    display: block;
    width: 100%;
    position: relative;
    float: left;
    clear: both;
}

.recent-entries.grid .listing-item {
    margin-bottom: 60px;
}

.recent-entries.grid .listing-item .image {
    display: inline-block;
    width: 100%;
    margin-bottom: 10px;
}

.recent-entries.grid .listing-item img {
    width: 100%;
    height: auto;
    margin: 0;
    float: left;
}

.recent-entries.grid .listing-item .title {
    font-weight: bold;
}

@media (min-width: 768px) {
    .recent-entries.grid .listing-item {
        width: 48%;
    }

    .recent-entries.grid .listing-item:nth-child(odd) {
        float: left;
        clear: left;
    }

    .recent-entries.grid .listing-item:nth-child(even) {
        float: right;
        clear: right;
    }

    .recent-entries.grid .listing-item .excerpt-dash {
        display: none;
    }

    .recent-entries.grid .listing-item .excerpt {
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
    }
}

This CSS will display a responsive layout that works on mobile devices:

wordpress list posts by modified date responsive

As well as on tablets and desktop computers:

list posts by modified date css

Note that it’s also possible to invoke a shortcode outside a post or page using PHP. For example, you can display a list of articles in a sidebar by inserting the following content in your theme:

<?php
echo do_shortcode('[display-posts orderby="modified"]');
?>

That said, many other plugins allow you to list your posts. Whether using a plug-in or PHP code, it’s possible to sort and filter articles in multiple ways. It is also possible to display additional information such as date, author, and excerpt.

You can find the complete list of parameters here.

Leave a Reply

This Website is Hosted by

Powered by RamNode

Disclosure: We receive a compensation from some of the companies whose products or services are presented on our website.