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 8 years, 11 months ago by dedsonus.
Viewing 3 posts - 1 through 3 (of 3 total)
-
AuthorPosts
-
December 30, 2015 at 7:54 am #175180dedsonusMember
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 #175192Davinder Singh KainthMemberRefer 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 ThemesDecember 30, 2015 at 10:00 am #175196dedsonusMemberI 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')) ),
-
AuthorPosts
Viewing 3 posts - 1 through 3 (of 3 total)
- The forum ‘General Discussion’ is closed to new topics and replies.