寻找另一种方法从wpdb获取最后一个帖子ID

时间:2013-07-20 02:32:58

标签: php wordpress wpdb

我需要从$ wpdb获取最后一个帖子ID。出于某种原因,$wpdb->insert_id仅返回零。获取最后一个帖子ID的另一种方法是什么?

3 个答案:

答案 0 :(得分:0)

insert_id是属性,而不是方法。不用()来试试。

答案 1 :(得分:0)

Wordpress在Docs中指定get_the_ID()"The Loop")函数,该函数通常用于迭代WP站点之外的帖子。在这种情况下,我们只运行它并在检索到最新的帖子ID(数组中的第一个ID)后中断。

<?php
require_once("path/to/your/wp-config.php");
$wp->init();
query_posts(array('post__in' => $postarray ));
while ( have_posts() ) : the_post();
    $most_current_id = get_the_ID();
    if ($most_current_id){
        echo "Most recent Post ID: $current_id";
        break;
    }
endwhile;

?>

答案 2 :(得分:0)

编写以下sql查询以获取最后一个帖子ID

$ posts_table = $ wpdb-&gt; prefix。“posts”;     $ first_post = $ wpdb-&gt; get_row(“SELECT MAX(ID)FROM $ posts_table WHERE post_status ='publish'”,ARRAY_A);

$ post_table返回上一篇文章..