这是我的代码:
我只想返回已登录用户的帖子。
我正在用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;
}
答案 0 :(得分:2)
据我了解,您的问题是获得“当前用户的职位”。然后是下面的代码。
1)您需要传递作者ID(当前用户ID)作为参数。
$current_user = wp_get_current_user();
$args = array(
'author' => $current_user->ID,
'orderby' => 'post_date',
'order' => 'ASC',
);
2)通过上面的参数是post,如下所示:
$current_user_posts = get_posts( $args );
希望对您有帮助。
答案 1 :(得分:1)
您也可以使用WP_Query()
$user = wp_get_current_user();
$query_args = array(
'post_type' => 'post',
'author' => $user->ID,
);
$query = new WP_Query($query_args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post = $query->post;
echo $post->post_title;
}
wp_reset_postdata();
}
else{
echo 'No posts found.';
}