Archives

Show Authors Their Own Posts in Admin

You can redirect the normal All Posts pages to a version that only displays the current user’s posts. (Similar to this post). Users will not be able to get back to the ‘all’ view, as in the other sample. Users are not prevented from changing the author ID in the URL and viewing another user’s posts.

add_action( 'load-edit.php', 'posts_for_current_author' );
function posts_for_current_author() {
	global $user_ID;

	/*if current user is an 'administrator' do nothing*/
	//if ( current_user_can( 'add_users' ) ) return;

	/*if current user is an 'administrator' or 'editor' do nothing*/
	//if ( current_user_can( 'edit_others_pages' ) ) return;

	if ( ! isset( $_GET['author'] ) ) {
		wp_redirect( add_query_arg( 'author', $user_ID ) );
		exit;
	}

}

Redirect When Search Query Only Returns One Match

Be careful when using this, as users may not expect this functionality.

If there’s only one match for a particular search query, you can save users the hassle of clicking by simply redirecting them to the page/post.

add_action('template_redirect', 'single_result');
function single_result() {
	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 ) );
			exit;
		}
	}
}

Redirect When Search Query is an Exact Title Match

You probably shouldn’t use this if you have really generic page/post titles, but it can be a really handy feature.

Basically, if a user searches for something that happens to be an exact match to a page or post you have, it’ll redirect the user to the page, rather than displaying search results.

add_action('template_redirect', 'seach_query_is_title');
function seach_query_is_title() {
	if (is_search()) {
		global $wp_query;
		if ( get_page_by_title( get_search_query(), 'OBJECT', 'post' ) ) {
			wp_redirect( get_permalink( get_page_by_title (get_search_query() )->ID ) );
		}
		elseif ( get_page_by_title( get_search_query(), 'OBJECT', 'page' ) ) {
			wp_redirect( get_permalink( get_page_by_title( get_search_query() )->ID ) );
		}
	}
}