Skip to content

Instantly share code, notes, and snippets.

@mmikkel
Created November 11, 2014 10:50
Show Gist options
  • Save mmikkel/ad92321a10201b11627c to your computer and use it in GitHub Desktop.
Save mmikkel/ad92321a10201b11627c to your computer and use it in GitHub Desktop.
TimberPost extension w/ post slug support
<?php
class MyPost extends TimberPost {
public function __construct( $post_id = null, $post_type = 'post' ) {
if ( is_string( $post_id ) ) {
// If $post_id is a string, we'll consider it a slug and try to get the ID
$args = array(
'name' => $post_id,
'post_type' => $post_type, // Because slugs aren't necessarily unique, we'll have to specify post type
'posts_per_page' => 1,
'no_found_rows' => true,
);
if ( $posts = get_posts( $args ) ) {
$post = array_shift( $posts );
$post_id = $post->ID;
}
}
parent::__construct( $post_id );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment