php 从帖子标题中获取 WordPress 帖子 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1536682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:00:05  来源:igfitidea点击:

Get WordPress Post ID from Post title

phpwordpress

提问by Aaron

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:

我正在开发的自定义 WordPress 主题有问题。这有点令人费解,但本质上,我需要做的是通过它的帖子标题获得一个帖子 ID。在伪代码中,它最好是这样的:

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.

提到的标题是一个静态引用,不是从 WordPress 中提取的,它已经出现在页面上。

Thanks in advance.

提前致谢。

回答by Michal Mau

Just a quick note for anyone who stumbles across this:
get_page_by_title()can now handle any post type.
The $post_typeparameter has been added in WP 3.0.

对于
偶然发现此问题的任何人,只需快速说明:get_page_by_title()现在可以处理任何帖子类型。
$post_type参数已在 WP 3.0 中添加。

回答by Aaron

Found a solution if anyone else struggles with this. Only posted the question out of desperation after 4 hours testing/Googling!

如果其他人遇到此问题,请找到解决方案。经过 4 小时的测试/谷歌搜索后,才绝望地发布了这个问题!

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;
}

Found at: http://sudarmuthu.com/blog/2009/09/18/retrieving-posts-and-pages-based-on-title-in-wordpress.html

位于:http: //sudarmuthu.com/blog/2009/09/18/retrieving-posts-and-pages-based-on-title-in-wordpress.html

回答by BrainBUG

Like Michal Mau mentioned:

就像 Michal Mau 提到的:

Use

$my_post = get_page_by_title( 'My Title', OBJECT, 'post' );
echo $my_post->post_content;

It's ( $page_title, $output, $post_type )to easily receive a post instead of a page.

这是( $page_title, $output, $post_type )为了轻松接收帖子而不是页面。

回答by Kundan SIngh

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);

回答by Ramkumar

you can use the following code as per [a link][http://codex.wordpress.org/Function_Reference/get_page_by_title]1)!

您可以按照 [a link][http://codex.wordpress.org/Function_Reference/get_page_by_title] 1)使用以下代码!

<?php 
$page = get_page_by_title( 'About' );
wp_list_pages( 'exclude=' . $page->ID );
?>

回答by Omdev

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

使用 wp 查询很容易从帖子标题中获取帖子 ID:

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;

回答by T.Todua

1) differ post_titleand post_namefrom each other. post_name maybe is the slug. post_title is the title of post.

1) post_titlepost_name彼此不同。post_name 可能是蛞蝓。post_title 是帖子的标题。

2)

2)

$titlee = "yourtitle";
echo $id = $wpdb->get_var("SELECT ID FROM $GLOBALS['wpdb']->posts WHERE post_name = $titlee");

回答by user1470261

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

获取帖子和页面 ID 的另一种方法是使用插件。

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

有一个插件,它的作用很简单,就是在你的所有页面、所有帖子、所有类别表中添加一列,并有一个 ID 的列标题......就在下面,你将看到所有页面/帖子该列中列出的 ID..

I think that should be very useful..

我觉得应该很有用。。

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

我经常使用这个插件,它非常轻巧。

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

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

回答by Erik Larsson

No need to use any type of SQL querys or plugin, use Wordpress standard functions for this

无需使用任何类型的 SQL 查询或插件,为此使用 Wordpress 标准函数

$page = get_page_by_title( 'Home' );
$page_id = $page->ID;