WordPress.org

Make WordPress Core

Ticket #14019: get_id_by_name.14019.diff

File get_id_by_name.14019.diff, 926 bytes (added by filosofo, 8 years ago)
  • wp-includes/post-template.php

     
    9292}
    9393
    9494/**
     95 * Retrieve the post ID from its name or slug.
     96 *
     97 * @since 3.1.0
     98 *
     99 * @param string $slug The name or slug of the post to retrieve.
     100 * @return int The ID of the retrieved post, or 0 if none found.
     101 */
     102function get_id_by_name( $slug = '' ) {
     103        if ( empty( $slug ) )
     104                return 0;
     105        $query = new WP_Query;
     106        $posts = (array) $query->query( array(
     107                'name' => $slug,
     108                'post_status' => 'publish',
     109                'post_type' => 'any',
     110                'showposts' => 1,
     111        ) );
     112       
     113        $post = array_shift( $posts );
     114
     115        if ( ! empty( $post->ID ) )
     116                return (int) $post->ID;
     117        else
     118                return 0;
     119}
     120
     121/**
    95122 * Retrieve post title.
    96123 *
    97124 * If the post is protected and the visitor is not an admin, then "Protected"