WordPress search__only_ Variables

Here’s another object-oriented mu-plugin that you can expand upon if needed. This adds the option to perform a search with “&search__only_titles” or “&search__only_content” in the url to search only titles or content of posts.

<?php

class rdc_search_object {
	
	function __construct() {
		add_filter( 'posts_search', 'filter__posts_search' );
	}
	
	function filter__posts_search( $query ) {
		$query_parts = explode( ' OR ', $query );
		
		if ( isset( $_GET['search__only_titles'] ) )
			return $query_parts[0] . '))';
		
		if ( isset( $_GET['search__only_content'] ) )
			return ' AND ((' . $query_parts[1];
		
		return $query;
	}
	
}

$rdc_search_object = new rdc_search_object();

// omit

Go ahead and use this juicy tidbit and let me know in the comments what creative use you found for it.

Leave a Reply