[Resolved] Child-theme PHP interferes with WP Site Health

Home Forums Support [Resolved] Child-theme PHP interferes with WP Site Health

Home Forums Support Child-theme PHP interferes with WP Site Health

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #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' );
    #1402888
    Leo
    Staff
    Customer Support

    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.

    #1402947
    NBC

    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.)

    #1403040
    NBC

    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.

    #1403042
    Leo
    Staff
    Customer Support

    Thanks for sharing!

    #1403054
    NBC

    No problem, Leo. Wanted to share and be helpful.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.