我想获取WordPress中自定义帖子类型的最后一个帖子ID。为此,我尝试了以下代码-
$args = array(
'post_type' =>'soto_property',
'posts_per_page' => 1,
'orderby'=>'post_date',
'order' => 'DESC',
);
$image_posts = get_posts($args);
foreach ( $image_posts as $post ) {
echo $post->ID;
}
但是,它总是向我返回此自定义帖子类型的第一个帖子ID。我也尝试过,'order'=>'ASC'
但是它给了我相同的结果。
我如何获得此CPT的最后一个帖子ID?
我已经使用$wpdb
查询获取了我最后插入的帖子ID ,如下所示:
global $wpdb;
$lastrowId=$wpdb->get_col( "SELECT ID FROM wp_posts where post_type='soto_property' ORDER BY post_date DESC " );
$lastPropertyId=$lastrowId[0];
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句