-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_by_slug.php
41 lines (37 loc) · 1.06 KB
/
get_by_slug.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
/**
* Retrieve a page given its slug.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $page_slug Page slug
* @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A.
* Default OBJECT.
* @param string|array $post_type Optional. Post type or array of post types. Default 'page'.
* @return WP_Post|null WP_Post on success or null on failure
*/
function get_page_by_slug( $page_slug, $output = OBJECT, $post_type = 'page' ) {
global $wpdb;
if ( is_array( $post_type ) ) {
$post_type = esc_sql( $post_type );
$post_type_in_string = "'" . implode( "','", $post_type ) . "'";
$sql = $wpdb->prepare( "
SELECT ID
FROM $wpdb->posts
WHERE post_name = %s
AND post_type IN ($post_type_in_string)
", $page_slug );
} else {
$sql = $wpdb->prepare( "
SELECT ID
FROM $wpdb->posts
WHERE post_name = %s
AND post_type = %s
", $page_slug, $post_type );
}
$page = $wpdb->get_var( $sql );
if ( $page )
return get_post( $page, $output );
return null;
}
?>