You need to define the variables within the function. Now they’re not defined, so both are set to 0, excluding every post.
add_filter(‘relevanssi_do_not_index’, ‘rlv_exclude_exp’, 10, 2);
function rlv_exclude_exp($exclude, $post_id) {
$todaysdate = date ( ‘Y-m-d’ );
$expire_date = get_field(‘end_date’, $post_id);
if ($expire_date >= $todaysdate) $exclude = true;
return $exclude;
}
Do note that this doesn’t do anything when the post actually expires, until the index is rebuilt or the post is saved. Add this to have a realtime effect, too:
add_filter('relevanssi_post_ok', 'rlv_exclude_exp_search', 10, 2);
function rlv_exclude_exp_search($include, $post_id) {
$todaysdate = date ( ‘Y-m-d’ );
$expire_date = get_field(‘end_date’, $post_id);
if ($expire_date >= $todaysdate) $include = false;
return $include;
}
Thanks so much for your quick response!
I tried both codes but the latter one is causing nothing to show up in the search even though posts are being indexed. And it seems expired posts are still getting indexed with either code.
I’m sure this is an error with my variables or how I have it setup; I’ve tried different variations and can’t seem to figure it out. Even if I could filter out posts that were published before 2016 that would be useful as it seems when rebuilding the index that the oldest posts are added first.
That’s how the function should work, but if it doesn’t, debug it. Change the latter function to this:
add_filter('relevanssi_post_ok', 'rlv_exclude_exp_search', 10, 2);
function rlv_exclude_exp_search($include, $post_id) {
$todaysdate = date ( 'Y-m-d' );
$expire_date = get_field('end_date', $post_id);
if ($expire_date >= $todaysdate) $include = false;
echo "<h1>Expire date: $expire_date | Today's date: $todaysdate</h1>";
echo "<h1>Include the post: $include</h1>";
exit();
return $include;
}
and run a search. What does it print out? Does it get the correct dates, and is the include correct?