Get WordPress Post ID from Post title

2019-01-30 18:05发布

I have an issue with a custom WordPress theme I'm developing. It's a bit convoluted, but essentially, what I need to do is get a Post Id by it's Post Title. In pseudo-code it would ideally be something like:

title = "foo";
post_id = get_post_id_where_title_is(title);

The title mentioned is a static reference not being pulled in from WordPress, it's already present on the page.

Thanks in advance.

标签: php wordpress
9条回答
三岁会撩人
2楼-- · 2019-01-30 18:35

May this will help you more by creating function so that you need not to repeat the code

function get_page_id_by_title($title)
{
$page = get_page_by_title($title);
return $page->ID;
}

$title = "your title";
get_page_id_by_title($title);
查看更多
\"骚年 ilove
3楼-- · 2019-01-30 18:39

Another way to get the post and page ID, is to use a plugin..

there is a plugin, that what it simply does, is just add a column to your all pages, all posts, all categories tables, and have a column title of ID...and right below, you will see all the page/post id listed in that column..

I think that should be very useful..

I use this plugin very frequently and it is very lightweight.

http://getyourblogready.com/?p=758

查看更多
做个烂人
4楼-- · 2019-01-30 18:44

it is easy to get the post id from post title using wp query:

global $wpdb;

$rw = $wpdb->get_row( $wpdb->prepare("select * from "your post table name" where post_title='your variable name or your post title'"));

echo $rw->ID;
查看更多
登录 后发表回答