• 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-

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter zyrq

    (@zyrq)

    nevr mind…
    again…

    /**
    * 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;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get ID from post title for shortcode’ is closed to new replies.