- This topic has 5 replies, 2 voices, and was last updated 6 months, 3 weeks ago by
NBC.
-
AuthorPosts
-
August 14, 2020 at 4:09 am #1402357
NBC
Back in 2017, I was setting up a portfolio site that used posts to display work samples. The forum thread about this is here: https://generatepress.com/forums/topic/randomizing-post-order-on-posts-page/
Part of the solution to my problem came from here: https://wordpress.stackexchange.com/questions/31647/is-it-possible-to-paginate-posts-correctly-that-are-random-ordered/33275#33275
Unfortunately, that code now creates another problem: It interferes with Site Health in WordPress. (The “session_start” was the clue.)
“An active PHP session was detected,” Site Health reports, among other things, chiding me for “critical information about your WordPress configuration and items that require your attention.”
My question is pretty simple: Does WP do a better job of randomizing post display than it did in 2017? Can I remove the following code from my child theme?
session_start(); add_filter('posts_orderby', 'edit_posts_orderby'); function edit_posts_orderby($orderby_statement) { $seed = $_SESSION['seed']; if (empty($seed)) { $seed = rand(); $_SESSION['seed'] = $seed; } $orderby_statement = 'RAND('.$seed.')'; return $orderby_statement; } function tu_rand_posts( $query ) { if ( $query->is_main_query() && ! is_admin() ) { $query->set( 'orderby', 'rand' ); } } add_action( 'pre_get_posts', 'tu_rand_posts' );
August 14, 2020 at 8:53 am #1402888Leo
StaffCustomer SupportHi there,
My question is pretty simple: Does WP do a better job of randomizing post display than it did in 2017? Can I remove the following code from my child theme?
I wouldn’t think so but not 100% sure.
You would need to check with WordPress’ support or simply try removing the code and see if it works for you.
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/August 14, 2020 at 9:32 am #1402947NBC
Thanks, Leo. I’ve posted over on wordpress.org, and I’ll come back to this thread with an answer when I get one. (I’m not marking it as resolved for that reason, if that’s OK.)
August 14, 2020 at 10:26 am #1403040NBC
So here’s the reply I received on wordpress.org
https://wordpress.org/support/topic/child-theme-post-randomizing-php-interferes-with-wp-site-health/#post-13257883
along with my reply thereto.I hope this helps others who are using this particular fix.
August 14, 2020 at 10:31 am #1403042Leo
StaffCustomer SupportThanks for sharing!
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/August 14, 2020 at 10:38 am #1403054NBC
No problem, Leo. Wanted to share and be helpful.
-
AuthorPosts
- You must be logged in to reply to this topic.