• Resolved joyner.cn

    (@joynercn)


    If I have the string ‘bob’, how do I get the title of the post with the permalink mysite.com/bob?

    Right now I’m trying:

    $value = 'bob';
    $thispost = get_post($value);
    echo '<li>' . $thispost->post_title . '</li>';

    However, that isn’t returning anything. Needless to say, $value is coming from somewhere else, but when I print $value directly, it’s the right text — so it’s the last step I’m unsure about.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    get_post() is mainly used to get the post object when we already know the post ID. It does not work with titles or slugs. The most flexible way to get a particular post is get_posts(), even though it is intended for getting multiple posts, it can get a single post just as well. Just remember that it returns an array of posts, even if there is only one post in the array.

    $value = 'bob';
    $thispost = get_posts("name=$value");
    echo '<li>' . $thispost[0]->post_title . '</li>';

    Thread Starter joyner.cn

    (@joynercn)

    That did it, thank you so much!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Getting a post from its permalink?’ is closed to new replies.