4 WordPress search tweaks

I don’t have plugins installed to enhance my blog’s search engine, unless you count Jetpack and the custom functions I use.

Recently, I played around with it on my own blog, curious to find something whilst omitting other results, and found something quite amazing!

Janepedia: Search results for jane
Not a page in sight (query)

The functions

If search results return one post, redirect to that post

// Redirect visitor to post/page if it is the only search result || sauce: https://www.paulund.co.uk/redirect-search-results-return-one-post

add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
	if (is_search()) {
		global $wp_query;
			if ($wp_query->post_count == 1 && $wp_query->max_num_pages == 1) {
				wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );

This also works for pages, but I omitted pages from displaying in search results.

Omit pages from search results

This is where it got tricky, because I wanted to omit pages and show only posts in the search results, but found the function affected my dashboard. This is okay with me, because I have a few pages and no need to search for them.

// Only posts show in search results
	function filter_search($query) {
	if ($query->is_search) {
		$query->set('post_type', 'post');
	return $query;
	add_filter('pre_get_posts', 'filter_search');

However, I had to add images to the search results function in order to be able to search for them in my Media Library. To achieve this, I created an array() inside the function variables, achieving the following:

// Only posts show in search results
		function filter_search($query) {
		if ($query->is_search) {
			$query->set('post_type', array( 'post', 'attachment'));
		return $query;
		add_filter('pre_get_posts', 'filter_search');

To keep images from appearing on their own, and to deter spam, I created image.php and filled it with the following:

<?php wp_redirect(get_permalink($post->post_parent)); ?>

This function redirects direct image URLs — which is helpful in the event of someone clicking to view the image page from Google — to the posts they are attached to. If unattached, the permalink results in a redirection issue.

Despite its occasional unsexiness, it helps me fight spam because there is no chance for any bots to leave comments on images. The Media Library gives each image its own page, but by doing this, images sharing slugs with my posts don’t cause any weird issues.

The file location of the images remain as-is upon upload, but their respective WordPress attachment permalinks will alter when attached to a post.

e.g. /pool-4ft-sign/ was attached to Jane Lately v.48, thus changed to /lately-48/pool-4ft-sign/, which redirects (thanks to the function) to /lately-48/.

Search hacks

Ever want to find something on someone else’s WordPress blog and get smacked with the posts that only mention that one time they…?

I present to you my latest amusement.

Exclude certain words

Google, and lots of other handy dandy search engines, let you modify your search results using a subtraction sign (-). You can do something similar on WordPress.

On Georgie’s blog, I first searched for tea.

Hey Georgie: Search results for tea

Up came the results, but I decided I didn’t want results pertaining to Seattle (for example purposes), so I chose to omit it. My new search query: tea -seattle.

Hey Georgie: Search results for tea -seattle

This successfully omits the post about Seattle from the results!

Looking for specific phrase

Using quotes (" and "), you can search for specific phrases and have results displayed.

However, if you do this on Janepedia and the results return one post, you’ll be redirected to that post.

So…if you put into search something specific, like ipsy [month abbrev.], you’ll likely get results pertaining to Ipsy and anything containing that month’s abbreviation.

However, if you search for it in quotes, i.e. "ipsy [month abbrev.]", you’re bound to be forwarded to precisely that post — if it exists.

Ergo, quoting for the unveiling of my Ipsy glambag for February forwards you straight to that page.

(I have to be verbose about it, or else I’ll screw up my live example results. 😭)

At least, for now. There may come a time when the year has to be added in order for you to be forwarded. 😅

They just can’t be combined so easily.

Unlike the popular, robust search engines fueled by Silicon Valley and/or Wall Street, the WordPress search engine will always search for what is in quotes.

So if you are searching for a sponsor-related guest post on Yes and Yes, but want to omit the “Web Time Wasters”, you cannot query like so:

sponsors -"web time wasters"

Instead, you’d be better off choosing the biggest keyword out of that phrase and using it to omit instead.

i.e. sponsors -wasters

That’s what I’ve found so far! I surmise there are other ways to play with it, but this is what has helped me most when it comes to finding stuff on both my and other people’s blogs.~

#LetsDiscuss is hosted by FYFA & ISAM.

Love this post?

Support me by subscribing to my blog and/or buying me a cuppa:

Leave a comment

Comments on this post

This feature is really cool! Sometimes, I just want a few key words without having to dig through dozens of posts. Thanks for sharing this! I am a bit convinced to bring back my search bar, hehe.

Reply to this »