• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Related posts – how to show only posts of last N. days?

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Related posts – how to show only posts of last N. days?

This topic is: not resolved

Tagged: related posts

  • This topic has 2 replies, 2 voices, and was last updated 10 years, 4 months ago by dedsonus.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • December 30, 2015 at 7:54 am #175180
    dedsonus
    Member

    Hi,
    how to limit related posts by last year or two?
    Thank you!
    I'm using this code in functions.php:

    /** WPT related post widget */
    function related_posts_categories() {
    if ( is_single ( ) ) {
    global $post;
    $count = 0;
    $postIDs = array( $post->ID );
    $related = '';
    $cats = wp_get_post_categories( $post->ID );
    $catIDs = array( );{
    foreach ( $cats as $cat ) {
    $catIDs[] = $cat;
    }
    $args = array(
    'category__in'          => $catIDs,
    'post__not_in'          => $postIDs,
    'showposts'             => 5,
    'ignore_sticky_posts'   => 0,
    'orderby'               => 'rand',
    'tax_query'             => array(
    array(
    'taxonomy'  => 'post_format',
    'field'     => 'slug',
    'terms'     => array(
    'post-format-link',
    'post-format-status',
    'post-format-aside',
    'post-format-quote' ),
    'operator' => 'NOT IN'
    )
    )
    );
    $cat_query = new WP_Query( $args );
    if ( $cat_query->have_posts() ) {
    while ( $cat_query->have_posts() ) {
    $cat_query->the_post();
    $related .= '<li><a href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to' . get_the_title() . '">' . get_the_title() . '</a></li>';
    }
    }
    }
    if ( $related ) {
    printf( '<div class="related-posts"><h4>related posts:</h4><ul>%s</ul></div>', $related );
    }
    wp_reset_query();
    }
    }
    add_action( 'genesis_after_entry_content', 'related_posts_categories' );
    December 30, 2015 at 9:29 am #175192
    Davinder Singh Kainth
    Member

    Refer discussion here - https://wordpress.org/support/topic/will-this-code-limit-the-posts-displayed-to-the-last-14-days


    Sunshine PRO genesis theme
    Need Genesis help? Davinder @ iGuiding Media | My Blog | Fresh Genesis Themes

    December 30, 2015 at 10:00 am #175196
    dedsonus
    Member

    I appreciate it. Just found the solution:

    /** WPT related post widget */
    function related_posts_categories() {
    if ( is_single ( ) ) {
    global $post;
    $count = 0;
    $postIDs = array( $post->ID );
    $related = '';
    $cats = wp_get_post_categories( $post->ID );
    $catIDs = array( );{
    foreach ( $cats as $cat ) {
    $catIDs[] = $cat;
    }
    $args = array(
    'category__in'          => $catIDs,
    'post__not_in'          => $postIDs,
    'showposts'             => 5,
    'ignore_sticky_posts'   => 0,
    'orderby'               => 'rand',
    'date_query' => array(
            'after' => date('Y-m-d', strtotime('-700 days')) 
        ),
    'tax_query'             => array(
    array(
    'taxonomy'  => 'post_format',
    'field'     => 'slug',
    'terms'     => array(
    'post-format-link',
    'post-format-status',
    'post-format-aside',
    'post-format-quote' ),
    'operator' => 'NOT IN'
    )
    )
    );
    $cat_query = new WP_Query( $args );
    if ( $cat_query->have_posts() ) {
    while ( $cat_query->have_posts() ) {
    $cat_query->the_post();
    $related .= '<li><a href="' . get_permalink() . '" rel="bookmark" title="' . get_the_title() . '">' . get_the_title() . '</a></li>';
    }
    }
    }
    if ( $related ) {
    printf( '<div class="related-posts"><h4>Related posts:</h4><ul>%s</ul></div>', $related );
    }
    wp_reset_query();
    }
    }
    add_action( 'genesis_after_entry_content', 'related_posts_categories' );

    I used this code to limit posts to 700 last days:

    'date_query' => array(
            'after' => date('Y-m-d', strtotime('-700 days')) 
        ),
  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble