get_posts-按作者ID获取所有帖子
-
-
从版本4.5.0开始不推荐使用get_currentuserinfo().替换为:`$ current_user=wp_get_current_user();`get_currentuserinfo() is deprecated since version 4.5.0. Replace with: `$current_user = wp_get_current_user();`
- 1
- 2017-05-15
- Christian Lescuyer
-
3 个回答
- 投票数
-
- 2013-08-12
我有点困惑.如果您想从posts数组中获取一个元素,则可以这样获取:
- 重置($ current_user_posts)-第一篇文章
- end($ current_user_posts)-后期发布
但是,如果您只想通过
get_posts()
获得一篇帖子,则可以使用posts_per_page
参数来限制结果.$args = array( 'author' => $current_user->ID, 'orderby' => 'post_date', 'order' => 'ASC', 'posts_per_page' => 1 );
有关您可以在 WP查询类参考页上获得的参数的更多信息(
get_posts()
与WP Query使用相同的参数.I'm a bit confused. If you want to get onlya element from the posts array you can get it like this:
- reset($current_user_posts) - first post
- end($current_user_posts) - lat post
But if you want to get just one post with the
get_posts()
you can use theposts_per_page
argument to limit the results.$args = array( 'author' => $current_user->ID, 'orderby' => 'post_date', 'order' => 'ASC', 'posts_per_page' => 1 );
More info about parameters you can get on WP Query Class Reference page (
get_posts()
takes same parameters as WP Query).-
您的$ args工作正常,但我没有得到您的第一个答案.如何使用$ current_user_posts.可以展示给我吗?your $args work fine but I don't get your first answer. How to use $current_user_posts. Could you show me?
- 1
- 2013-08-12
- kindo
-
如果要打印第一篇文章的标题,应使用:`echo $ current_user_posts [0] ['title']`."标题"是您从阵列中获得所需内容的关键.您可以通过print_r(array_keys($ current_user_posts))获得的密钥的完整列表. "如何使用"取决于您要如何使用它.If you want to print the title of the first post you should use: `echo $current_user_posts[0]['title']`. The 'title' is the key for what you need from array. The full list of keys you cang get with `print_r(array_keys($current_user_posts))`. "How to use" it depends on what you want to do with it.
- 0
- 2013-08-12
- Marin Bînzari
-
获取作者的第一篇文章的IDget the author's first post's id
- 0
- 2013-08-12
- kindo
-
您可以通过以下方式获取ID:$ current_user_posts [0] ['ID']You can get the id with: $current_user_posts[0]['ID']
- 0
- 2013-08-12
- Marin Bînzari
-
@kindo,有帮助吗?这是您需要的答案吗?@kindo, did it helped? Is this the answer you needed?
- 0
- 2013-08-12
- Marin Bînzari
-
$ current_user_posts [0] ['ID']不起作用.但是第一个添加"numberposts"或"posts_per_page"(使用等于)的解决方案可以正常工作.ty$current_user_posts[0]['ID'] does not work. but the first solution with adding 'numberposts' or 'posts_per_page' (used equal) works fine. ty
- 0
- 2013-08-12
- kindo
-
@kindo,对不起,忘记了get_posts()返回post对象数组.使用`$ current_user_posts [0]-> ID`@kindo, Sorry, forgot that `get_posts()` returns array of post objects. Use `$current_user_posts[0]->ID`
- 0
- 2013-08-12
- Marin Bînzari
-
现在,最后一个解决方案也适用.The last solution now also works.
- 0
- 2013-08-12
- kindo
-
- 2016-09-09
global $current_user; $args = array( 'author' => $current_user->ID, 'orderby' => 'post_date', 'order' => 'ASC', 'posts_per_page' => -1 // no limit ); $current_user_posts = get_posts( $args ); $total = count($current_user_posts);
并仅循环当前用户的帖子
global $current_user; $args = array( 'author' => $current_user->ID, 'orderby' => 'post_date', 'order' => 'ASC', 'posts_per_page' => -1 // no limit ); $current_user_posts = get_posts( $args ); $total = count($current_user_posts);
and just loop the current user posts
-
除了发布代码外,您还能解释上述代码的作用吗,谢谢您Can you also explain what the above code does in addtion to posting the code, it will be helpful, thanks
- 0
- 2016-09-09
- bravokeyl
-
- 2018-07-08
其工作原理(wp4.9.7)
$user_id = get_current_user_id(); $args=array( 'post_type' => 'POSTTYPE', 'post_status' => 'publish', 'posts_per_page' => 1, 'author' => $user_id ); $current_user_posts = get_posts( $args ); $total = count($current_user_posts); wp_die( '<pre>' . $total . '</pre>' );
its work by (wp4.9.7)
$user_id = get_current_user_id(); $args=array( 'post_type' => 'POSTTYPE', 'post_status' => 'publish', 'posts_per_page' => 1, 'author' => $user_id ); $current_user_posts = get_posts( $args ); $total = count($current_user_posts); wp_die( '<pre>' . $total . '</pre>' );
我想按特定作者ID(当前用户)获取所有帖子.稍后,我要选择该用户(ASC)发表的第一篇文章. 我猜我在get_posts中使用的参数不正确,是吗?$ current_user_posts 始终包含一个数组,其中所有博客帖子都位于多个不同的WP_Post对象中.