How to show a post if was published less than two hours ago?

I want to show posts only that were posted less that 2 hours ago and have the custom field “¿directo?” with the value “Sí”.

These are my progresses, but they don’t work:

Read More

Functions.php:

function is_time_to_show_post($hora, $minutos, $dia, $mes)
{
    $now = current_time( 'timestamp' );
    $hour_now = date( 'H', $now ); // 24h format
    $minutes_now = date( 'i', $now ); // 24h format
    $month_now = date( 'n', $now ); // 24h format
    if($hour_now > $hora +2){ //same day, have passed 3 hours or more
        return 'no';
    }
    if(($hour_now - $hora <= 2 && $hour_now - $hora) != 0 && ($minutes_now - $minutos) >= 0 ){ //same day, have passed two days or more
        return 'no';
    }
    if($minutes_now <= 59 - $minutos && $hour_now == 0){ //in less than two hours, day change
        return 'no';
    }
    else{
        return 'yes';
    }
}

For example, tests in search.php:

                <div class="row js-masonry">
                    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                        <?php
                        $directo = get_post_meta( the_post().get_the_ID(), '¿directo?', true);
                        $mostrar = true;
                        if($directo   == 'Sí'        &&      is_time_to_show_post(   the_post().get_post_time('G', false, null,false)   , the_post().get_post_time('i', false, null,false)  ) == 'yes'     ){
                        }else if($directo   == 'Sí'){
                            $mostrar = false;
                        }

                        if($mostrar){
                            echo '<div class="col-6 col-sm-6 col-lg-4 extract">';
                            get_template_part( 'content', the_post().get_post_format() );
                            echo '</div>';
                        }

                        ?>
                    <?php endwhile; wp_reset_query(); endif; ?>


                </div>

                <!-- ============================= /Posts -->

Related posts

Leave a Reply