relevanssi_post_ok doesn’t affect indexing, only searching. For searching, your function should work. If you’re seeing older posts in the results, I would recommend checking that the dates are correct: add some var_dump() clauses to your function and see what you’re getting.
For indexing, you can use relevanssi_do_not_index, which has the same parameters (but inverted return values, so true means the post is not indexed).
Awesome, that did the trick, here’s what I used:
add_filter('relevanssi_do_not_index', 'index_the_recent', 11, 2);
function index_the_recent($post_ok, $doc) {
$date = get_the_time('Y-m-d H:i:s', $doc);
$date_filter = date('Y-m-d H:i:s', strtotime('-30 days'));
if ($date < $date_filter) $post_ok = true;
return $post_ok;
}
Do note that that code doesn’t automatically remove old posts from the index, it just prevents them from being indexed in the future. Unless you occasionally rebuild the index, the old posts will remain in the index until they are saved or the index is rebuilt.
Hmm, yes thank you for pointing that out. I would like the old posts removed without manually pressing the rebuild index button. Would using something like this:
if (!wp_next_scheduled('relevanssi_build_index')) {
wp_schedule_event( time(), 'daily', 'relevanssi_build_index' );
}
automatically remove the old posts?
Yes. There are some problems with this approach, especially if your index is big, but since you only index the last 30 days, this should work just fine.