0

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);
?>

2 Answers 2

1

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.

3
  • 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
    Commented Sep 7, 2011 at 7:14
  • Solved it by removing some blankspace in my php file! Thanks!
    – Richard
    Commented Sep 7, 2011 at 8:33
  • Glad it worked, though I'd still advise having some kind of fallback in case there is no matching post :) Commented Sep 7, 2011 at 14:23
0

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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