This cannot work with the $_SESSION['watched'] , because the $_SESSION variable is not related to the MySQL table and as a result, the formatted query did not know how many messages it should return.
I see only one solution for the correct solution to this problem:
You need to create another table in the database that can store session_id and post_id variables. For example, we call this table wp_watched .
For example, the structure of our table will be:
CREATE TABLE wp_watched ( `watched_ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `watched_SID` varchar(255) DEFAULT NULL, `watched_POST_ID` bigint(20) NOT NULL DEFAULT '0', PRIMARY KEY (`watched_ID`) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
And the data will be stored in our table as follows:
`watched_ID` | `watched_SID` | `watched_POST_ID` --------------------------------------------------------------------------------- 1 | 098f6bcd4621d373cade4e832627b4f6 | 345 2 | 88578edf8458ce06fbc5bb76a58c5ca4 | 123 3 | 1a36591bceec49c832079e270d7e8b73 | 5 4 | 2b1d3a6249c2d223c620393fa6420868 | 98 5 | 793560d7e4ef27db84dfe776fed9cea5 | 12 ...
Then you need to rewrite your code for the session for something like this using custom queries :
$watchedrows = $wpdb->get_row("SELECT watched_ID FROM wp_watched WHERE watched_SID = " . session_id() . " AND watched_POST_ID = " . $post->ID); if(!$watchedrows->watched_ID) { $wpdb->insert('wp_watched', array('watched_SID' => session_id(), 'watched_POST_ID' => $post->ID), array('%s', '%d')); }
Then you need to create your own custom query to receive messages based on the connection between the wp_posts table and the wp_watched table:
$querystr = " SELECT $wpdb->posts.* FROM $wpdb->posts, wp_watched WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_date < NOW() AND wp_watched.watched_SID = " . session_id() . " AND $wpdb->posts.ID != wp_watched.watched_POST_ID ORDER BY RAND() LIMIT 1"; $randompost = $wpdb->get_row($querystr, OBJECT);
Then, if everything is fine, the $randompost variable will contain your random entry.
Not sure if the code I provide works, I have not tested it. Some SQL queries may need to be rewritten.
Just to show you the direction you need to go.