Two WordPress tweaks

I have been searching for a way to make my personal blog more user friendly. In fact, this is what I keep doing, searching for better ways to tweak my WordPress blog.

The first tweak was related to search results: I have noticed that the sorting is by date, in reverse order – from older to newer. As older posts tend to be less relevant for the visitor, I found a way to change it.

In order to do this, you have to add the following code to your theme’s functions.php file:

function my_search_query( $query ) {
// not an admin page and is the main query
if ( !is_admin() && $query->is_main_query() ) {
if ( is_search() ) {
$query->set( 'orderby', 'date' );
}
}
}
add_action( 'pre_get_posts', 'my_search_query' );

The second tweak is, actually, something that bothered me for a long time, but I didn’t wanted to use a plugin in order to solve the problem. So, again, adding code to functions.php seems to be the faster and better way to do it.

Adding the following code to functions.php will remove short words from the post URL, making them more SEO-friendly. For example, after adding the code, try to create a new post with a title like “This is a test to check if short words are removed”. You will see that the URL has changed to “this-test-check-short-words-are-removed”.

add_filter('sanitize_title', 'remove_short_words');</p>
function remove_short_words($slug) {
if (!is_admin()) return $slug;
$slug = explode('-', $slug);
foreach ($slug as $k => $word) {
if (strlen($word) < 3) {
unset($slug[$k]);
}
}
return implode('-', $slug);
}

These two tweaks for WordPress made my life better. I hope yours too! 🙂

Both tweaks were found on two forums, here and here.

Leave a Reply

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