首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在WP_Query中,get_current_user_id( )不返回自定义post类型的数据

在WP_Query中,get_current_user_id( )不返回自定义post类型的数据
EN

Stack Overflow用户
提问于 2019-05-29 12:36:25
回答 1查看 145关注 0票数 0

我想从我的自定义post类型中获取数据(注意),这就是我使用WP_Query()的原因,它工作得很好,但是当我只想获取与登录的用户相关的数据时,我使用的是author => get_current_user_id(),现在我的网页没有显示任何内容

代码语言:javascript
运行
复制
<?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();

        ?>

它没有显示任何内容(空白)

我的备注自定义帖子类型

//备注帖子类型

代码语言:javascript
运行
复制
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'

]);

代码语言:javascript
运行
复制
<?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();

?>
EN

回答 1

Stack Overflow用户

发布于 2019-05-29 13:09:59

尝试以下代码。

代码语言:javascript
运行
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56353176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档