Add Snippet To Project
Navigating through a sea of posts within a specific category or tag can sometimes become overwhelming for your website visitors. What if there’s only one post under a category or tag? Wouldn’t it be easier if your website could redirect your visitors straight to that single post? That’s exactly what we’ll be addressing in this article. We’ll help you grasp the concept and guide you through the steps to set up a redirection to a single post if a category or tag on your WordPress site contains only one post. Get ready for a better user experience!
function wpturbo_single_post_redirect() {
if (is_category() || is_tag()) {
global $wp_query;
if ($wp_query->post_count == 1) {
wp_redirect(get_permalink($wp_query->posts['0']->ID));
exit;
}
}
}
add_action( 'template_redirect', 'wpturbo_single_post_redirect' );
The provided PHP code snippet is used to redirect web users to a single post if there’s only one post within a specific tag or category in WordPress. Here’s how the code works.
The code begins with the function wpturbo_single_post_redirect()
. It’s this function that carries the code to perform the redirect when necessary.
function wpturbo_single_post_redirect() {
Inside the function, the first thing that it does is that it checks if the current page is a category or tag page.
if (is_category() || is_tag()) {
WordPress provides is_category()
and is_tag()
functions. These functions are Conditional Tags, which are used in WordPress to test various conditions. Here, they are used to test if a Category archive page or a Tag archive page is being displayed, respectively.
If the page is a category or tag archive, the code proceeds to check the number of posts in the current query using $wp_query->post_count
. This returns the total number of posts being shown in a given page template (including any additional queries on the given page.)
global $wp_query;
if ($wp_query->post_count == 1) {
Here, the global $wp_query;
line is what enables the function to access the $wp_query
object, which contains information about the current page request.
If the conditional statement evaluates to true, that is, if there is only one post, the wp_redirect
function is executed and the user is redirected to the permalink of the single post in the category or tag.
wp_redirect(get_permalink($wp_query->posts['0']->ID));
exit;
The get_permalink($wp_query->posts['0']->ID)
is used to obtain the URL of that single post in the query where redirection should happen. The $wp_query->posts['0']->ID
, returns the ID of the first (and only) post in the $wp_query
object. Finally, the exit;
command stops script execution, ensuring that the redirect takes place immediately.
The wpturbo_single_post_redirect()
function is then added to the 'template_redirect'
action hook through the add_action
function.
add_action( 'template_redirect', 'wpturbo_single_post_redirect' );
The template_redirect
action hook happens before the template specified in the query is loaded. Since we want to redirect before the template is rendered in the browser, this hook is the perfect place to perform that action.
In summary, this code snippet effectively checks if a tag or category archive page has only one post, and if it does, it redirects to the single post page, skipping the archive page altogether.