Created
November 11, 2014 10:50
-
-
Save mmikkel/ad92321a10201b11627c to your computer and use it in GitHub Desktop.
TimberPost extension w/ post slug support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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