To specifically exclude posts with “2024” in their titles using a code snippet, you can add this to your theme’s functions.php
file:
function exclude_title_posts( $query ) {
if ( $query->is_search && !is_admin() ) {
$query->set('post_title_not_like', '2024');
}
}
function modify_search_where( $where, $query ) {
global $wpdb;
if ( $title_not_like = $query->get( 'post_title_not_like' ) ) {
$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title NOT LIKE %s ", '%' . $wpdb->esc_like( $title_not_like ) . '%' );
}
return $where;
}
add_filter( 'posts_where', 'modify_search_where', 10, 2 );
add_action( 'pre_get_posts', 'exclude_title_posts' );
This snippet modifies the WordPress query when it’s a search query, adding a condition to exclude posts whose titles contain “2024”.