問題描述

我想通過某些作者 id(當前用户) 獲取所有帖子。之後,我想選擇這位用户 (ASC) 發表的第一篇文章。我想我不會在 get_posts 中使用正確的參數,我是嗎? $ current_user_posts 始終包含一個 Array,其中包含多個不同 WP_Post 對象中的所有博客文章。

global $current_user;
get_currentuserinfo();

$args = array(
    'author'        =>  $current_user->ID, // I could also use $user_ID, right?
    'orderby'       =>  'post_date',
    'order'         =>  'ASC'
    );

// get his posts 'ASC'
$current_user_posts = get_posts( $args );

最佳解決方案

我有點困惑如果你想從 posts 數組中只得到一個元素,你可以這樣得到它:

  • 重置 ($ current_user_posts) – 第一篇文章

  • 結束 ($ current_user_posts) – 拉桿

但是如果您想要使用 get_posts()只收到一個帖子,您可以使用 posts_per_page 參數來限制結果。

$args = array(
    'author'        =>  $current_user->ID,
    'orderby'       =>  'post_date',
    'order'         =>  'ASC',
    'posts_per_page' => 1
    );

關於 WP Query Class Reference 頁面的參數的更多信息 (get_posts()與 WP Query 相同) 。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。