Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi bebibu. First thing is you need to be running a child theme. If you already have a child theme, but no functions.php file, you’ll need to create one using FTP or cPanel on your host. Add this to the top of that file:

    <?php
    /* ------------------------------------------------------------------------- *
     *  Custom functions
    /* ------------------------------------------------------------------------- */
    	// Add your custom functions here, or overwrite existing ones. Read more how to use:
    	// https://codex.www.ads-software.com/Child_Themes

    If you’re not currently running a child theme, download the preconfigured child theme from the theme web site. After activating the child theme you’ll have a style.css file and functions.php file.

    Then you would copy the entire parent theme function alx_related_posts() to your child theme functions.php file. I’ve included it here for reference:

    function alx_related_posts() {
    	wp_reset_postdata();
    	global $post;
    
    	// Define shared post arguments
    	$args = array(
    		'no_found_rows'			=> true,
    		'update_post_meta_cache'	=> false,
    		'update_post_term_cache'	=> false,
    		'ignore_sticky_posts'		=> 1,
    		'orderby'			=> 'rand',
    		'post__not_in'			=> array($post->ID),
    		'posts_per_page'		=> 3
    	);
    	// Related by categories
    	if ( ot_get_option('related-posts') == 'categories' ) {
    
    		$cats = get_post_meta($post->ID, 'related-cat', true);
    
    		if ( !$cats ) {
    			$cats = wp_get_post_categories($post->ID, array('fields'=>'ids'));
    			$args['category__in'] = $cats;
    		} else {
    			$args['cat'] = $cats;
    		}
    	}
    	// Related by tags
    	if ( ot_get_option('related-posts') == 'tags' ) {
    
    		$tags = get_post_meta($post->ID, 'related-tag', true);
    
    		if ( !$tags ) {
    			$tags = wp_get_post_tags($post->ID, array('fields'=>'ids'));
    			$args['tag__in'] = $tags;
    		} else {
    			$args['tag_slug__in'] = explode(',', $tags);
    		}
    		if ( !$tags ) { $break = true; }
    	}
    
    	$query = !isset($break)?new WP_Query($args):new WP_Query;
    	return $query;
    }

    At the top of the function you’ll see the arguments that are used to filter the posts query. You would add a date_query argument to the query arguments array so it looks like this:

    // Define shared post arguments
    $args = array(
    	'date_query' => array(
    		array(
    			'column' => 'post_date_gmt',
    			'after' => '2 months ago',
    			),
    		),
    	'no_found_rows'			=> true,
    	'update_post_meta_cache'	=> false,
    	'update_post_term_cache'	=> false,
    	'ignore_sticky_posts'		=> 1,
    	'orderby'			=> 'rand',
    	'post__not_in'			=> array($post->ID),
    	'posts_per_page'		=> 3
    );
    // Related by categories

    This will bring in only those posts created within the last two months.

    Let me know if you have any questions.

    Thread Starter bebibu

    (@bebibu)

    Awesome! It’s work.

    Thank you, bdbrown.

    Case closed.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to limit related posts to a certain time?’ is closed to new replies.