0

So I've tried numerous times to do this myself but nothing is working, so now I"m turning to you find folks at StackOverflow.

I have a serialized array of posts viewed by user called "flaged". Now in the front-end this works fine and when a user views a post, it posts their user ID, the post ID and the date viewed. I can then cross-match this and populate a list in wp-admin which displays the user, post viewed, etc in rows. This only works for $current_user though, so say the administrator logs in, they can only see the posts they've viewed. I need this to work for all users.

Here's the code for viewing the list of posts viewed in the admin. Please share any suggestions you may have.

<?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;
}
  • Why vote down instead of telling me what you don't understand about the question? I'm happy to clarify. I've also researched this to now end to no avail. – Justin Hubbard Jun 20 '13 at 18:36
  • For anyone who comes across this, I solved the issue by instead of using `WP_Query`, I took out the entire while loop and wrapped the results in a `foreach($postids as $id)` then simply displayed the data using `$user->display_name`, etc. I hope this helps somebody later on down the road – Justin Hubbard Jun 21 '13 at 16:08

1 Answers1

0

For anyone who comes across this, I solved the issue by instead of using WP_Query, I took out the entire while loop and wrapped the results in a foreach($postids as $id) then simply displayed the data using $user->display_name, etc. I hope this helps somebody later on down the road