我想从我的自定义帖子类型(注释)中获取数据,所以这就是为什么我使用WP_Query()的原因,它工作正常,但是当我想获取仅与登录用户有关的数据时,我正在使用作者=> get_current_user_id()现在我的网页什么都没有显示
<?php
$args = array(
'author' => get_current_user_id(), // before using this it was working fine
'posts_per_page' => -1,
'post_type' => 'note',
);
$userNotes = new WP_Query( $args );
if ($userNotes->have_posts()): while ($userNotes->have_posts()) : $userNotes->the_post(); ?>
<li>
<input class="note-title-field" value="<?php echo esc_attr(get_the_title()); ?>">
<span class="edit-note"><i class="fa fa-pencil" aria-hidden="true"></i> Edit</span>
<span class="delete-note"><i class="fa fa-trash-o" aria-hidden="true"></i> Delete</span>
<textarea class="note-body-field"><?php echo esc_attr(get_the_content()); ?></textarea>
</li>
<?php
endwhile; endif;
wp_reset_postdata();
wp_reset_query();
?>
没有显示任何内容(空白)
我的笔记自定义帖子类型
//注释帖子类型
register_post_type('note',[
'show_in_rest' => true,
'supports' => ['title','editor'],
'public' => false,
'show_ui' =>true,
'labels' => [
'name'=>'Notes',
'add_new_item' =>'Add New Note',
'edit_item' => 'Edit Note',
'all_items' =>'All Notes',
'singular_name' =>'Note'
],
'menu_icon' => 'dashicons-welcome-write-blog'
]);
<?php
if (!is_user_logged_in()) {
wp_redirect(esc_url(site_url('/')));
exit;
}
get_header();
pageBanner();
?>
<div class="container container--narrow page-section">
<ul class="min-list link-list" id="my-notes">
<?php
$args = array(
'posts_per_page' => '-1',
'post_type' => 'note',
'author' => get_current_user_id()
);
$userNotes = new WP_Query($args);
while($userNotes->have_posts()) : $userNotes->the_post();
?>
<li>
<input class="note-title-field" value="<?php echo esc_attr(get_the_title()); ?>">
<span class="edit-note"><i class="fa fa-pencil" aria-hidden="true"></i> Edit</span>
<span class="delete-note"><i class="fa fa-trash-o" aria-hidden="true"></i> Delete</span>
<textarea class="note-body-field"><?php echo esc_attr(get_the_content()); ?></textarea>
</li>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
</div>
<?php
get_footer();
?>
答案 0 :(得分:0)
尝试以下代码。
if ( is_user_logged_in() ):
global $current_user;
get_currentuserinfo();
$author_query = array('posts_per_page' => '-1','post_type' => 'note','author' => $current_user->ID);
$author_posts = new WP_Query($author_query);
while($author_posts->have_posts()) : $author_posts->the_post();
?>
<a href="<?php the_permalink(); ?>" title="<?php get_title(); ?>"></a>
<?php
endwhile;
endif;