Wordpress shows a random entry that has not been shown before.

I created this very simple function after a WordPress documentary, and as I understand it, this code should work just fine

query_posts(array('orderby' => 'rand', 'post__not_in' => $_SESSION['watched'], 'showposts' => 1)); 

and the code for the session is no more advanced and then

 if(!in_array($post->ID, $_SESSION['watched'])){ array_push($_SESSION['watched'],$post->ID); } 

If you know any other solution or how to fix this particular one all the answers are very appreciated ... If I missed something, just tell me and I will add this information.

+4
source share
2 answers

This question on wordpress.org seems relevant to your question - obviously, there are some rather strange problems with post__not_in .

Try the following:

 function removeSeenPosts ($where) { global $wpdb; if (count($_SESSION['watched']) > 0) { $where .= ' AND ' . $wpdb->posts . '.ID NOT IN(' . implode (',', $_SESSION['watched']) . ') '; } return $where; } add_filter('posts_where', 'removeSeenPosts'); query_posts(array('orderby' => 'rand', 'showposts' => 1)); remove_filter('posts_where', 'removeSeenPosts'); 

If this does not help (in fact I suspect), the problem is clearly related to the message store in the session.

Two things come to mind:

  • Can $_SESSION be filled after something but sent headers? I do not remember...
  • Storing messages in the right cookie seems like a viable alternative and, as an added bonus, will make storage persistent throughout the sessions.

Unsold implementation as a plugin:

 function setViewedPostCookies() { if (is_single()) { global $wp_query; if (!isset($_COOKIE['watched'])) { $excluded_posts = array(); } else { $excluded_posts = implode(',', $_COOKIE['watched']); } $excluded_posts[] = $wp_query->post->ID; $excluded_posts = array_unique($excluded_posts); setcookie('watched', explode(',', $excluded_posts), time() + 3600000, '/'); } } add_action( 'get_header', 'setViewedPostCookies' ); 
+2
source

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.

+1
source

All Articles