Support » Plugin: Relevanssi - A Better Search » Only index posts not older than…

  • Hi,

    is it possible to just index posts not older than e.g. half a year?
    But just for post_type post? So I want to keep all other post types in the index, no matter how old they are, but I want that normal posts are only in the index if they are newer than half a year.

    Thanks,
    Till

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

    (@msaari)

    Add this to your theme functions.php:

    add_filter( 'relevanssi_do_not_index', 'rlv_only_old_posts', 10, 2 );
    function rlv_only_old_posts( $block, $post_id ) {
        if ( 'post' === relevanssi_get_post_type( $post_id ) ) {
            $post_time = get_the_time( 'U', $post_id );
            $time_now  = date( 'U' );
            if ( $post_time > $time_now - 6 * MONTH_IN_SECONDS ) {
                $block = true;
            }
        }
        return $block;
    }

    Now when a post is indexed and it has the post type post, Relevanssi will check the date and if it’s within the last six months, Relevanssi won’t index the post.

    However, there’s a catch. Relevanssi won’t notice when the six months have passed and won’t automatically reindex the posts. In order to get the older posts back in the index, you need to either go save them, or rebuild the Relevanssi index. Setting up an automatic reindexing every week or so would probably be a good idea with this.

    Okay thank you!

    Unfortunately it doesn’t seem to work…

    Since I’ve put in the code into my functions.php, I only get this when I want to create a new index:

    Indexed 0 posts (total 0), processed 0 / 158451.
    Indexed 0 posts (total 0), processed 0 / 158451.
    Indexed 0 posts (total 0), processed 0 / 158451.
    Indexed 0 posts (total 0), processed 0 / 158451.
    Indexed 0 posts (total 0), processed 0 / 158451.
    Indexed 0 posts (total 0), processed 0 / 158451.
    … and so on

    its alsways at 0 indexed posts and remaining time is infinity…

    Plugin Author Mikko Saari

    (@msaari)

    Ok, for starters, you have 158 451 posts. You really shouldn’t be using Relevanssi to begin with.

    Second, if you have lots of posts published within 6 months, it’ll take a while before Relevanssi gets through them. Eventually Relevanssi should get past them, and start actually indexing posts.

    Here’s a better version:

    add_filter( 'relevanssi_indexing_restriction', 'rlv_only_old_posts' );
    function rlv_only_old_posts( $restriction ) {
    	$time_limit   = date( 'U' ) - 6 * MONTH_IN_SECONDS;
    	$restriction .= " AND UNIX_TIMESTAMP( post.post_date ) < '$time_limit' ";
    	return $restriction;
    }

    This will work much better with those quantities of posts. I still think you should not be using Relevanssi, but instead something that is hosted outside your own server. Relevanssi just isn’t built for those kinds of numbers.

    tillmeier

    (@tillmeier)

    Okay thank you! But is there an option to have this restriction only for the post_type “post” and not for the other post types? So I only want to index posts not older than 6 month, but for all other post types I don’t need a time limit…

    Plugin Author Mikko Saari

    (@msaari)

    I haven’t tried this, but this might work:

    add_filter( 'relevanssi_indexing_restriction', 'rlv_only_old_posts' );
    function rlv_only_old_posts( $restriction ) {
    	$time_limit   = date( 'U' ) - 6 * MONTH_IN_SECONDS;
    	$restriction .= " AND ( (post.post_type = 'post' AND UNIX_TIMESTAMP( post.post_date ) < '$time_limit') OR (post.post_type != 'post') ";
    	return $restriction;
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Only index posts not older than…’ is closed to new replies.