正在寻找从wpdb获取最后一个post ID的其他方法


looking for an other way to get the last post ID from wpdb

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

insert_id是一个属性,而不是一个方法。尝试不使用()。

Wordpress在"循环"中指定了一个get_the_ID()(Docs)函数,该函数通常用于迭代WP站点外的帖子。在这种情况下,我们只需遍历它,并在检索到最新的post 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;
?>

编写以下sql查询以获取最后一个post-id

$posts_table=$wpdb->前缀。"帖子";$first_post=$wpdb->get_row("从$posts_table WHERE postrongtatus='publish'中选择最大值(ID)",ARRAY_A);

$post_table返回最后一个帖子。。