Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 从登录用户获取帖子_Wordpress - Fatal编程技术网

Wordpress 从登录用户获取帖子

Wordpress 从登录用户获取帖子,wordpress,Wordpress,我有两种帖子类型。一个是另一个的父帖子类型。我当前正在尝试获取当前登录用户的父帖子。问题是数组正在获取所有post父级。如何做到这一点?谢谢 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-te

我有两种帖子类型。一个是另一个的父帖子类型。我当前正在尝试获取当前登录用户的父帖子。问题是数组正在获取所有post父级。如何做到这一点?谢谢

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>';
}
}
add_操作('add_meta_box_listing_type'、'my_add_meta_box');
功能我的添加元框($post){
添加元框(
“my-listing_type-parent”,
__(“媒体”,“示例textdomain”),
“我的列表类型父元框”,
$post->列表类型,
"一边",,
“核心”
);
}
函数我的列表类型父元框($post){
$parents=获取帖子(
排列(
“post_type”=>“media”,
'orderby'=>'title',
“订单”=>“ASC”,
“numberposts”=>-1
“post_author”=>???
)
);
如果(!空($parents)){
回显“”;/!重要!不要更改“父id”名称属性。
foreach($parents作为$parent){
printf(“%s”,esc_attr($parent->ID),selected($parent->ID,$post->post_parent,false),esc_html($parent->post_title));
}
回声';
}
}

不确定您是否解决了此问题,但如果使用
wp\u get\u current\u user()
函数,您将获得当前用户ID。然后只需稍微调整
get\u posts()
数组,以请求“author”而不是“post\u author”

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