簡體   English   中英

獲取登錄用戶的帖子

[英]Get posts from logged-in user

我有兩種職位類型。 一種是另一種的父帖子類型。 我目前正在嘗試獲取當前登錄用戶的父帖子。 問題在於該數組正在獲取所有的職位上級。 如何才能做到這一點? 謝謝!

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>';
}
}

不知道是否解決了這個問題,但是如果使用wp_get_current_user()函數,將獲得當前的用戶ID。 然后,您只需要稍微調整get_posts()數組,即可請求“作者”而不是“ post_author”。

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

希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM