Wordpress $ user-> ID而不是$ current_user-> ID

时间:2013-06-20 18:03:55

标签: php wordpress posts userinfo

所以我已经多次尝试过这样做,但没有任何工作,所以现在我转向你找到StackOverflow的人。

我有一个由用户查看的序列化帖子数组,名为“flaged”。现在在前端这很好用,当用户查看帖子时,它会发布他们的用户ID,帖子ID和查看日期。然后我可以交叉匹配它并在wp-admin中填充一个列表,该列表在行中显示用户,发布后查看等。这仅适用于$ current_user,所以说管理员登录后,他们只能看到他们查看过的帖子。我需要这个适用于所有用户。

以下是查看管理员中查看的帖子列表的代码。请分享您的任何建议。

<?php

ini_set('display_errors',1); 
 error_reporting(E_ALL);

function searchForId($id, $array) {
   foreach ($array as $key => $val) {
       if ($val['post_id'] === $id) {
           return $array[$key]['date'];
       }
   }
   return null;
}

function user_views() {
    //global $current_user;
    //get_currentuserinfo();
    //$user_flags = get_user_meta($current_user->ID, 'flaged', true);
    $user_flags_query = new WP_User_Query(array('meta_key' => 'flaged'));
    $user_flags = $user_flags_query->get_results();

    $views_table = '<table id="user-views" class="wp-list-table widefat fixed user-views" cellspacing="0"> 
        <thead>
            <tr>
                <th scope="col" id="postviewed" class="manage-column column-postviewed" style="">
                    <span>Posts Viewed</span><span class="sorting-indicator"></span>
                </th>
                <th scope="col" id="filename" class="manage-column column-filename" style="">
                    <span>File Name</span><span class="sorting-indicator"></span>
                </th>
                <th scope="col" id="username" class="manage-column column-username" style="">
                    <span>Name</span><span class="sorting-indicator"></span>
                </th>
                <th scope="col" id="company" class="manage-column column-company" style="">
                    <span>Company</span><span class="sorting-indicator"></span>
                </th>
                <th scope="col" id="address" class="manage-column column-address" style="">
                    <span>Address</span><span class="sorting-indicator"></span>
                </th>
                <th scope="col" id="city" class="manage-column column-city" style="">
                    <span>City</span><span class="sorting-indicator"></span>
                </th>
                <th scope="col" id="state" class="manage-column column-state" style="">
                    <span>State</span><span class="sorting-indicator"></span>
                </th>
                <th scope="col" id="zip" class="manage-column column-zip" style="">
                    <span>Zip</span><span class="sorting-indicator"></span>
                </th>
                <th scope="col" id="email" class="manage-column column-email" style="">
                    <span>Email</span><span class="sorting-indicator"></span>
                </th>
                <th scope="col" id="ddate" class="manage-column column-ddate" style="">
                    <span>Date Downloaded</span><span class="sorting-indicator"></span>
                </th>
            </tr>
        </thead>
        <tbody id="the-list">';
        foreach($user_flags as $user){
            $user_info = get_user_meta($user->ID, 'flaged', true);
            if($user_info == null){ continue; }
            $postids = array();
            foreach($user_info as $row){
                $postids[] = $row['post_id'];
            }
            $my_q = new WP_Query(array('post__in' => $postids ));
            print_r($my_q);
            if ($my_q->have_posts()){
                while ( $my_q->have_posts() ) { $my_q->the_post();
             $views_table .= '<tr id="post-' . get_the_ID() .'">
                <td>
                    '. get_the_title().'<br />';
                    if(the_modified_date('Y-m-d','','',FALSE) < get_the_date('Y-m-d')) {
                    $views_table .= '<span>Published: '. the_date('','','',false) .'</span> |';
                    } else {
                    $views_table .= '<span>Modified: '. the_modified_date('','','',false) .'</span>';
                    } 
                $views_table .= '</td>
                <td>';
                    if(the_modified_date('Y-m-d','','',FALSE) > get_the_date('Y-m-d')) {
                    $views_table .= '<span>Addendum</span>';
                    } else {
                    $views_table .= '<span>Proposal Documents</span>';  
                    }
                $views_table .= '</td>';
                '<td>
                    '. $user->first_name . ' ' . $user->last_name .'
                </td>
                <td>
                    '. get_user_meta($user->ID, 'compnay', true).'
                </td>
                <td>
                    '. get_user_meta($user->ID, 'address', true).'
                </td>
                <td>
                    '. get_user_meta($user->ID, 'city', true).'
                </td>
                <td>
                    '. get_user_meta($user->ID, 'st', true).'
                </td>
                <td>
                   '.  get_user_meta($user->ID, 'zip', true).'
                </td>
                <td>
                    '. get_user_meta($user->ID, 'email', true).'
                </td>
                <td>
                    '. searchForID(get_the_ID(), $user_flags) .'
                </td>
            </tr>';
            }
        }else{
         $views_table .= 'nothing found';
        }
        wp_reset_query();
    }
    $views_table .= '</tbody></table>';
    $views_table .= '</div><!-- .wrap -->';

    return $views_table;
}

1 个答案:

答案 0 :(得分:0)

对于遇到此问题的任何人,我通过代替使用WP_Query来解决问题,我取出整个while循环并将结果包装在foreach($ postids as $ id)中,然后使用$ user-显示数据&gt; display_name等。我希望这会在以后帮助某人