Archived topic
Child-theme PHP interferes with WP Site Health
5 replies · Started by NBC on August 14, 2020
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' );
Hi 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.
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.)
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.
Thanks for sharing!
No problem, Leo. Wanted to share and be helpful.