Skip to main contentSkip to footer

Is there a way to exclude anything before 2016 from search results?

Yes. There are two approaches to this. If you never want to see anything old in the results, it’s best to filter in indexing with relevanssi_indexing_restriction. Add this to your site:

add_filter( 'relevanssi_indexing_restriction', 'rlv_exclude_old_posts' );
function rlv_exclude_old_posts( $restriction ) {
	global $wpdb;
	$restriction['mysql']  .= " AND YEAR(post.post_date) >= 2016 ";
	$restriction['reason'] .= ' Post too old';
	return $restriction;
}

Note that the restriction explains which posts are included in the index, so that’s why the year is >= 2016 and not < 2016.

To deindex posts from the news post type that are older than one year, you can use:

add_filter( 'relevanssi_indexing_restriction', 'rlv_exclude_old_news' );
function rlv_exclude_old_news( $restriction ) {
    $restriction['mysql']  .= ' AND ( ( post.post_date >= CURDATE() - INTERVAL 1 YEAR '
      . " AND post.post_type = 'news' ) OR ( post.post_type != 'news' ) ) ";
    $restriction['reason'] .= ' News post too old';
    return $restriction;
}

If, on the other hand, you only want to apply this filter in searching, you can use the WP_Query date parameters like this:

add_filter( 'relevanssi_modify_wp_query', 'rlv_date_filter' );

/**
 * Adds a date filter to the search query.
 *
 * @param WP_Query $query The query object.
 *
 * @return WP_Query The modified query object.
 */
function rlv_date_filter( $query ) {
    $date_query = array(
        'after'     => 'January 1st, 2016',
        'inclusive' => true,
    );
    $query->set( 'date_query', $date_query );
    return $query;
}

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Sorting search results
If you want something else than the default relevancy ranking, you can use orderby and order parameters for the search…
bbPress: Private Groups support
Private Groups is a plugin that makes bbPress forum groups private. Relevanssi, however, doesn’t understand that privacy and will show…
WP Event Manager

Using Relevanssi with WP Event Manager requires you to adjust the search process in WP Event Manager a bit. Fortunately, the plugin has good filters you can use. Add this to your site: add_filter( ‘get_event_listings_query_args’, function( $args ) { $args[‘relevanssi’] = true; return $args; } ); This will adjust the……event listing query arguments to switch on the Relevanssi flag, causing Relevanssi to process the results. This way you’ll get the Relevanssi results in the WP Event Manager event listing search. These Relevanssi results will also be cached by the WP Event Manager query caching. Thanks to dustyyy on WP…

Related Posts:

Comment Section:

2 Comments. Leave new

  • Hey guys,
    A client of ours is thinking on getting the premium plugin for his site.
    But we have a couple questions:
    — Is it possible to exlude / include specific pages / posts, etc?
    — for the search results page, would it be possible to show the page / post meta description?
    — Is the search resutls page the core WP results? Can we tweak it as we need, for example adding filters to it?

    Thanks in advance

    Reply
    • Joao, yes to all. Relevanssi has tools for excluding and including pages, either with filters or (in Premium) with post editor controls. Search results are shown with the search results template from your theme, so you can adjust that as much as you want to.

      It’s best if you install Relevanssi from the plugin repo, that’ll give you a good overview of how things work, and you may notice you don’t even need the Premium version.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed