WP_Query中的get_current_user_id()未返回自定义帖子类型的数据

我想从我的自定义帖子类型(注释)中获取数据, 所以这就是为什么我使用WP_Query(), 它工作正常, 但是当我只想获取与登录用户有关的数据时, 我正在使用author => 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();

?>

#1


请尝试以下代码。

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;

来源:

https://www.srcmini02.com/63593.html

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?