---
title: "Display Older Posts at Random in a MailPoet Newsletter"
description: "With this code snippet, create a shortcode in MailPoet to showcase five random articles that are older than one year in your newsletter's content."
url: "https://happybits.ca/display-older-posts-at-random-in-a-mailpoet-newsletter/"
type: "post"
date: "2022-08-20T17:56:25-04:00"
modified: "2026-04-13T00:05:17-04:00"
author: "Michael Krakowiak"
---

# Display Older Posts at Random in a MailPoet Newsletter

This code snippet will generate a shortcode \[custom:mp-random-posts\] that you can add to your newsletter in MailPoet to show a list of (five in this case) posts older than one year. Every subscriber will receive a unique list of articles.

Of course, you can modify the _date\_query_ or _posts\_per\_page_ values in the _$args_ variable. Feel free to modify the name of the shortcode in the code below, having in mind that MailPoet requires it to follow the naming structure: \[custom:xxxxxxxx\].

To use the code, add it to the _functions.php_ file in your theme or via a plugin like [Code Snippets](https://wordpress.org/plugins/code-snippets/) or my favourite [WPCodeBox](https://wpcodebox.com/?ref=201) (affiliate link.)

Finally, add the generated shortcode to your newsletter’s text box in MailPoet and use the Preview button to see if everything works as intended.

```
add_filter('mailpoet_newsletter_shortcode', 'mailpoet_custom_shortcode', 10, 6);

function mailpoet_custom_shortcode($shortcode, $newsletter, $subscriber, $queue, $newsletter_body, $arguments) {
    if ($shortcode !== '[custom:mp-random-posts]') return $shortcode;
  
    $args = array(
        'post_type' => 'post',
        'orderby'   => 'rand',
        'posts_per_page' => 5,
        'date_query' => array(
            array(
                'column' => 'post_date_gmt',
                'before' => '1 year ago',
            ),
        ),
    );
 
    $the_query = new WP_Query( $args );
 
    if ( $the_query->have_posts() ) {
    $string .= '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            $string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
        }
        $string .= '</ul>';
        wp_reset_postdata();
    } else {
        $string .= 'no posts found';
    }

    return $string; 
}
```

```
add_filter('mailpoet_newsletter_shortcode', 'mailpoet_custom_shortcode', 10, 6);

function mailpoet_custom_shortcode($shortcode, $newsletter, $subscriber, $queue, $newsletter_body, $arguments) {
    if ($shortcode !== '[custom:mp-random-posts]') return $shortcode;
  
    $args = array(
        'post_type' => 'post',
        'orderby'   => 'rand',
        'posts_per_page' => 5,
        'date_query' => array(
            array(
                'column' => 'post_date_gmt',
                'before' => '1 year ago',
            ),
        ),
    );
 
    $the_query = new WP_Query( $args );
 
    if ( $the_query->have_posts() ) {
    $string .= '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            $string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
        }
        $string .= '</ul>';
        wp_reset_postdata();
    } else {
        $string .= 'no posts found';
    }

    return $string; 
}
```
