code to exclude posts on homepage

this code in function.php

function get_hidden_posts() {
global $WP_Query;
$now_minus2 = date('Y-m-d H:i:s', strtotime('-120 minute', current_time('timestamp')));
$args = array(
    'date_query' => array(
        array(
            'before' => $now_minus2
        )
    ),
    'meta_query' => array(
        array(
            'key' => '┬┐directo?',
            'value' => 'Si',
            'compare' => '='
        )
    ),
    'posts_per_page' => -1,
    'fields' => 'ids'
);
$query_post_to_hide = new WP_Query($args);
$post_to_hide = array();
while ( $query_post_to_hide->have_posts() ) {
	$query_post_to_hide->the_post();
	$post_to_hide[] = get_the_ID();
}
return $post_to_hide;

}

function exclude_hidden_posts( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post__not_in', get_hidden_posts() );
    }
}
add_action( 'pre_get_posts', 'exclude_hidden_posts' );

 

Leave a Reply

Your email address will not be published. Required fields are marked *