In a recent project I was working with a Custom Post Type called Videos. In this specific layout, the single video page would show the embedded YouTube video with some other arbitrary information as well as a list of all other videos available. Instead of having an archive page showing all videos, and then duplicating that layout on the single post page under the main video it seemed logical to simply redirect the user to the first post if they happen to visit the video archive page.
This simple little technique will allow you to automatically redirect users from the post archive page for a custom post type directly to the first post of that post type.
<?php
/**
* Redirect to first post from CPT
*/
$args = array(
'orderby' => 'menu_order',
'post_type' => 'YOUR-POST-TYPE',
'posts_per_page' => 1
);
$loop = query_posts($args);
if (have_posts()) :
wp_redirect(get_permalink(), 302);exit();
endif;
This little snippet will go in the archive template for your custom post type. If you don’t have one you can create one by creating a file called archive-YOURCPT.php and placing it in the root of your theme or child theme. (Replace YOURCPT with your post type – in my case the file is archive-videos.php)
This works by querying your custom post type for a single post, and then redirecting to the permalink of that post when the page loads. A simple, effective way to point your traffic to the first post of your custom post type.
Additional Redirect Options
This technique could be modified to redirect in a variety of ways. A few ideas would be:
- Redirect to first post with a specific category
- Redirect to a random post
- Redirect to a custom landing page
Some simple modifications to the redirect could make this handy for a variety of tasks. Enjoy!
Awesome.
Work like charm.
Thanks so much for a great tip.
Hi
I need bit help with this code.
In my custom single post i have search from. When search don’t find any data it redirects to the latest regular post. How can i stop redirect to regular post?
Thanks
Hey!
What file have you added the code to? Did you add it to only a custom post type template or the index.php? Also, does your theme include a search.php? The only thing, without doing any troubleshooting, that I can think of is you don’t have a search.php and you put the code in the index.php which is the fallback for the search.
You could wrap the code in an if statement to check if the current query is a search using the is_search() check.
Hi
Thanks for your response.
I am using Underscores foundation.
I added this code in my cpt archive (archive-magazine.php) and taxonomy (taxonomy-magazine_category.php) templates and everything working fine here.
Now I have created cpt search and create new search template (search-magazine.php). And put this code again in my search template. So that search can be redirect to the recent post. Search works almost fine. But if search don’t find any post from the cpt it redirects to the regular recent post. I want stop redirect to the regular post and show any custom message or redirect to the custom 404 page.
Thanks
Without seeing any of your code I would suggest adding an “else” to the statement so if it returns no results you can redirect to 404 or enter a message. Something like:
if (have_posts()) :
wp_redirect(get_permalink(), 302);exit();
else :
** do something here **
endif;
‘menu_order’,
‘post_type’ => ‘magazine’,
‘posts_per_page’ => 1
);
$loop = query_posts($args);
if (have_posts()):
wp_redirect(get_permalink() , 302);
exit();
endif;
if (have_posts()): ?>
<?php
echo '’;
single_cat_title();
echo ”;
?>
<?php
get_footer();
Can you please edit the code for me if you custom post type don’t have any post it should redirect to the 404 page.
Thanks
Interesting solution! If you want to redirect your visitors only after clicking a button, you could use the plugin Redirect URL to Post, https://wordpress.org/plugins/redirect-url-to-post/
Good tip!