• I’m trying to use the relevanssi_do_not_index function to filter out posts that have expired via an ACF custom field, but I can’t seem to get it working. Every time I try to use the function then 0 posts get indexed.

    This is the meta query I’m using to remove expired posts from my posts page:
    ‘key’ => ‘end_date’,
    ‘value’ => $todaysdate,
    ‘compare’ => ‘>=’,

    So I’ve tried this, but again it leaves the index at 0:

    add_filter(‘relevanssi_do_not_index’, ‘rlv_exclude_exp’, 10, 2);

    $todaysdate = date ( ‘Y-m-d’ );
    $expire_date = get_field(‘end_date’, $post_id);

    function rlv_exclude_exp($exclude, $post_id) {
    if ($expire_date >= $todaysdate) $exclude = true;
    return $exclude;
    }

    Any help would be appreciated ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Mikko Saari

    (@msaari)

    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;
    }
    Thread Starter shayleesmith

    (@shayleesmith)

    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.

    Plugin Author Mikko Saari

    (@msaari)

    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?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Filter out by custom field’ is closed to new replies.