Home Forums Newsletter Plugin Support Filter WP posts by date

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #355849
    User
    Participant

    I am testing Newsletter Plugin and am very impressed!

    Can you confirm that there is no way to filter WP posts by date? I must be able to do this to prevent “stale” posts from appearing in the newsletter.

    I’m hoping there is a way!

    Thanks,
    Dan Hotchkiss

    #356015
    Michael
    Keymaster

    Hello Dan,

    thanks for the kind words. At the moment no, there’s no way to filter posts by date. Can you please explain to me what you mean by “stale” content?

    Thanks,
    Michael

    #356021
    User
    Participant

    Hi Michael,

    I send a weekly e-newsletter based on our website at http://www.uumiddleboro.org. Our minister writes a monthly column, which stays on the website for 11 days and then disappears. I do to do something similar in the weekly newsletter, so the same column does not appear repeatedly after it has gone “stale.” In addition to choosing a category and limiting the number of posts shown, I want to limit the amount of time before they disappear.

    To automate the disappearance of a post from the website, I use the code snippet below, which enables me to add to any query block a CSS class called since-xx-days-ago that chooses only posts that are less than xx days old.

    I hope this clarifies what I am looking for! It is similar to the “Range” setting in the Events Calendar block, but applied to the publication date of the post.

    Thanks,
    Dan

    add_filter( ‘generateblocks_query_wp_query_args’, function( $query_args, $attributes ) {
    if ( is_admin() || empty( $attributes[‘className’] ) ) {
    return $query_args;
    }
    // Look for a class like: since-20-days-ago, since-7-days-ago, etc.
    if ( preg_match( ‘/since-(\d+)-days-ago/’, $attributes[‘className’], $matches ) ) {
    $days = absint( $matches[1] ); // e.g. 20
    $query_args[‘date_query’] = array(
    array(
    ‘after’ => “{$days} days ago”,
    ),
    );
    }
    return $query_args;
    }, 10, 2 );

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