php 在wordpress中通过id检查帖子的存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9638375/
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-26 07:19:55  来源:igfitidea点击:

Check existence of a post by id in wordpress

phpwordpress

提问by Igor Parra

I have an id:

我有一个身:

$id = 151;

I want to check existence like this:

我想像这样检查存在:

$content = get_post($id);
if ($content)
    echo "post $id already exists";
else
    echo "post $id does not exists or was deleted";

But in the WP forums always seems prefer ask to DB:

但在 WP 论坛中,似乎总是比 DB 更喜欢问:

global $wpdb;
$post_exists = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE id = '" . $id . "'", 'ARRAY_A');
if ($post_exists)
    echo "post $id exists";
else
    echo "post $id does not exist";

So which is the best method? Indeed I prefer the ease of the first one.

那么最好的方法是什么?事实上,我更喜欢第一个的轻松。

采纳答案by dldnh

I think if they give you a function to a call, you call it; use the database for things that aren't provided by a function.

我想如果他们给你一个调用函数,你就调用它;将数据库用于函数未提供的内容。

回答by csag

I think the best is to query the database directly as less as possible

我觉得最好是尽量少直接查询数据库

You can use the get_post_status() function.

您可以使用 get_post_status() 函数。

It returns false if the post doesn't exist

如果帖子不存在则返回 false

if ( get_post_status ( $post_id ) ) {
    // do stuff
}

or, to be sure that the post is published

或者,为了确保帖子已发布

if ( 'publish' == get_post_status ( $post_id ) ) {
    // do stuff
}

http://codex.wordpress.org/Function_Reference/get_post_status

http://codex.wordpress.org/Function_Reference/get_post_status

回答by Dnyanesh

if( is_null(get_post($id))){

      echo "post $id does not exists or was deleted";

}else{

       echo "post $id already exists";

}

回答by AlFra

I know this is very old post, but still... If you want to check if post with certain id exists you can also use get_permalink wordpress function.

我知道这是很老的帖子,但仍然......如果你想检查是否存在具有特定 id 的帖子,你也可以使用 get_permalink wordpress 函数。

Example:

例子:

if( get_permalink( $post_id ) ):
echo "does exist";
else:
echo "does not exist";
endif;

Notice that get_permalink function also returns page, attachment links and not only posts.

请注意,get_permalink 函数还返回页面、附件链接,而不仅仅是帖子。

回答by davidjbullock

I would prefer querying the database over the get_post call.

我更喜欢通过 get_post 调用查询数据库。

The post content may be of great size. Querying and pushing the entire content of the post into a string just to check to see if it exists is incredibly wasteful and inefficient.

帖子内容可能很大。查询并将帖子的整个内容推送到一个字符串中只是为了检查它是否存在是非常浪费和低效的。

If you don't want to query the database directly, maybe using get_post_field to pull one of the smaller fields would be just as effective, but less wasteful.

如果您不想直接查询数据库,也许使用 get_post_field 来拉取较小的字段之一同样有效,但浪费更少。

回答by mattavatar

Use the WP_Queryclass for an efficient, SQL-free solution:

使用WP_Query类获得高效、无 SQL 的解决方案:

$id = 151;

$post_exists = (new WP_Query(['post_type' => 'any', 'p'=>$id]))->found_posts > 0;

if ($post_exists)
    echo "post $id already exists";
else
    echo "post $id does not exists or was deleted";

Note that both get_post_statusand get_permalinkload extraneous data.

请注意,get_post_statusget_permalink 都加载无关数据。