Last active
August 29, 2015 14:06
-
-
Save nash-ye/43ec717d61c795201180 to your computer and use it in GitHub Desktop.
Retrieves post data given a post name, type and parent ID.
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 | |
/** | |
* Retrieves post data given a post name, type and parent ID. | |
* | |
* @return WP_Post|NULL | |
*/ | |
function get_post_by_name( array $args ) { | |
global $wpdb; | |
$args = array_merge( array( | |
'name' => '', | |
'type' => 'post', | |
'parent' => 0, | |
'output' => OBJECT, | |
), $args ); | |
if ( ! $args['name'] || ! $args['type'] ) { | |
return; | |
} | |
$args['parent'] = intval( $args['parent'] ); | |
if ( $args['parent'] < 0 ) { | |
return; | |
} | |
$sql = $wpdb->prepare( " | |
SELECT ID | |
FROM $wpdb->posts | |
WHERE post_name = %s | |
AND post_parent = %d | |
AND post_type = %s | |
", $args['name'], $args['parent'], $args['type'] ); | |
$id = (int) $wpdb->get_var( $sql ); | |
if ( ! empty( $id ) ) { | |
return get_post( $id, $args['output'] ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment