Forums

get ID from post title for shortcode (3 posts)

  1. zyrq
    Member
    Posted 4 years ago #

    I'm setting up shortcodes to include a specific post.
    This works ok if I know the ID of the post: [myshortcode=192] where 192 is the ID of the post: get_post(192)
    However, I would like a shortcode like this: [myshortcode=shorty5] where shorty5 is the title of the post.

    Basically the shortcodes are in a category called shortcodes, user adds posts to that category to have new shortcodes [myshortcode=shorty6] etc.
    I dont want the user to 'go and find the ID of the post'.

    So, my question is: how can I use the title in the shortcodes instead of the ID -or- how can I get the postID from the title?
    -thanks-

  2. zyrq
    Member
    Posted 4 years ago #

    nevr mind...
    again...

  3. codeasily
    Member
    Posted 3 years ago #

    /**
    * Retrieve a post given its title.
    *
    * @uses $wpdb
    *
    * @param string $post_title Page title
    * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A.
    * @return mixed
    */
    function get_post_by_title($page_title, $output = OBJECT) {
        global $wpdb;
            $post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='post'", $page_title ));
            if ( $post )
                return get_post($post, $output);
    
        return null;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic