从登录用户获取帖子

时间:2014-12-10 00:12:41

标签: wordpress

我有两种后期类型。一个是另一个的父类型。我目前正在尝试获取当前登录用户的父帖子。问题是阵列正在获得所有后期父母。如何才能做到这一点?谢谢!

add_action( 'add_meta_boxes_listing_type', 'my_add_meta_boxes' );


function my_add_meta_boxes( $post ) {

add_meta_box(
    'my-listing_type-parent',
    __( 'Media', 'example-textdomain' ),
    'my_listing_type_parent_meta_box',
    $post->listing_type,
    'side',
    'core'
);
}

function my_listing_type_parent_meta_box( $post ) {

$parents = get_posts(
    array(
        'post_type'   => 'media', 
        'orderby'     => 'title', 
        'order'       => 'ASC', 
        'numberposts' => -1
        'post_author' => ???
    )
);

if ( !empty( $parents ) ) {

    echo '<select name="parent_id" class="widefat">'; // !Important! Don't change the 'parent_id' name attribute.

    foreach ( $parents as $parent ) {
        printf( '<option value="%s"%s>%s</option>', esc_attr( $parent->ID ), selected( $parent->ID, $post->post_parent, false ), esc_html( $parent->post_title ) );
    }

    echo '</select>';
}
}

1 个答案:

答案 0 :(得分:0)

不确定您是否解决了这个问题,但如果使用wp_get_current_user()功能,您将获得当前用户ID。然后,您只需稍微调整get_posts()数组,就可以要求“作者”#39;而不是&#39; post_author&#39;。

$user = wp_get_current_user();
$parents = get_posts(
array(
    'post_type'   => 'media', 
    'orderby'     => 'title', 
    'order'       => 'ASC', 
    'numberposts' => -1,
    'post_author' => $user->ID
    )
);

希望有所帮助

相关问题