0

所以我自己尝试了很多次,但没有任何效果,所以现在我转向你在 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;
}
4

1 回答 1

0

对于遇到此问题的任何人,我通过不使用 WP_Query 解决了这个问题,我取出了整个 while 循环并将结果包装在 foreach($postids as $id) 中,然后简单地使用 $user->display_name 显示数据,等等。我希望这对以后的人有所帮助

于 2013-09-15T07:18:42.567 回答