Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this template file called Events. The purpose of this file is just to display the latest post with custom type Event. I have solved it with a very ugly solution. There must be a simpler and more correct way to do this. If you check my code im sure you understand what im trying to accomplish :)

Thanks in advance!

<?php
/*
 * Template Name: Event
*/
?>

<?php
$permalink = get_permalink($wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_type = 'event' ORDER BY post_date DESC LIMIT 1"));
?>
<html>
<head>
<script type="text/javascript">
window.location = "<?php echo $permalink; ?>";
</script>
</head>
<body>
</body>
</html>

Cleaner solution:

<?php
/*
 * Template Name: Event
*/
$permalink = get_permalink($wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_type = 'event' ORDER BY post_date DESC LIMIT 1"));
header('Location: '.$permalink);
?>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

If you want to use the proper archive page (e.g. example.com/events), make sure that you register your custom post type with 'has_archive' => true and then create an archive-events.php in your theme (changing events to whatever you've named your CPT).

To show just the latest post, put the following before you call your loop stuff:

global $query_string; query_posts( $query_string . '&posts_per_page=1' );

If you just want to redirect to that latest one, which seems less elegant, try putting the above right at the beginning, starting the loop, and then use PHP header() to redirect, probably using 302 as the reason. Be sure that there is no output (even empty lines outside of PHP code) before you call header(). Also be sure to have some kind of fallback in case there is no matching post for the query.

share|improve this answer
    
Well i just want to redirect to the latest post, i tried using the code in my example but instead of the html and window.location javascript i used header. That resulted in an error saying "headers already sent". Any ideas? –  Richard Sep 7 '11 at 7:14
    
Solved it by removing some blankspace in my php file! Thanks! –  Richard Sep 7 '11 at 8:33
    
Glad it worked, though I'd still advise having some kind of fallback in case there is no matching post :) –  helenhousandi Sep 7 '11 at 14:23

You do not need to do anything other than register your post type appropriately: See http://codex.wordpress.org/Function_Reference/register_post_type#Parameters

with publicly_queryable = true (I think the default is false)

Then it will automatically have a template. It will use the single post if there is no custom template.

http://codex.wordpress.org/Template_Hierarchy#Single_Post_display.

See also http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress

Working Example here: (uses default template single post. Has a filter to add some event data in to the content for event post types): http://test.icalevents.com/event/bunny-hop/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.