I'm trying to exclude articles if are older than 2 years, related articles by category, but it just doesn't work.

        <?php $args = array('date_query' => array('post_date_gmt', 'before' => '24 month ago' ), 
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID),
'orderby'=>'rand', 'numberposts' => 4, 'post__not_in' => array($post->ID) )));
if( $related ) foreach( $related as $post ) {
        setup_postdata($post); ?>
    <a class=''  href='<?php the_permalink(); ?>'   >
    <?php the_title(); ?>
        <?php } wp_reset_postdata(); ?>
  • don't try to exclude posts older than 2 years, exclusion is expensive and slow. It's much much easier to include posts younger than 2 years.. I would also avoid rand as it's very very slow and expensive. Can you format your code? It's very difficult to read.
    – Tom J Nowell
    Dec 29, 2022 at 1:54


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.