Forum Replies Created

Viewing 1 post (of 1 total)
  • Author
    Posts
  • in reply to: Filter WP posts by date #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 1 post (of 1 total)