首页 > 解决方案 > 我希望 get_posts 只返回登录用户的帖子

问题描述

这是我的代码:

我只想返回登录用户的帖子。

我在 WordPress 重力表单中填充下拉表单。

$current_user = _wp_get_current_user(); 
function populate_posts( $form) {
   foreach ( $form['fields'] as &$field ) {
       if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
         continue;
       }
       $posts = get_posts( 'post_type=credit cards&numberposts=-1&post_status=publish&author=$current_user' );
       $choices = array();
       foreach ( $posts as $post ) {
         $choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
       }
       $field->placeholder = 'Select Credit Card';
       $field->choices = $choices;
   }

标签: wordpress

解决方案


据我了解,您的问题是获取“当前用户的帖子”。然后这是下面的代码。

1)您需要将作者 ID(当前用户 ID)作为参数传递。

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

2)通过以上参数发布如下:

$current_user_posts = get_posts( $args );

我希望这可以帮助你。


推荐阅读