WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Disable random post function on mobilesite (5 posts)

  1. Yake
    Member
    Posted 4 months ago #

    Hello!

    I have a function that makes the order of my posts appear random.

    It looks like this

    session_start();
    
    add_filter( 'posts_orderby', 'randomise_with_pagination' );
    function randomise_with_pagination( $orderby ) {

    I would like to disable this function on the mobile site. But im not sure how to do this, do you guys have any tips?

    Regards
    Jakob

  2. bcworkz
    Member
    Posted 4 months ago #

    Put if ( ! wp_is_mobile() ) in front of add_filter() :)

  3. Yake
    Member
    Posted 4 months ago #

    Thank you! Works like a charm.

    If you dont mind me asking, why does this work? Trying to learn more.

    /Yake

  4. bcworkz
    Member
    Posted 4 months ago #

    The resulting code line is saying in plain English "If the requesting device is NOT a mobile, add in the randomise filter."

    The '!' is the PHP NOT operator, reversing any boolean logic. wp_is_mobile() simply looks in the user agent field sent by a device's browser for certain key words like "Android", "BlackBerry", etc. So your randomise function is only added to the orderby filter when no such keywords are found.

    The orderby filter inserts anything returned by a callback function after the "ORDER BY" directive of the SQL query being processed.

  5. Yake
    Member
    Posted 4 months ago #

    Aha, i see. Thank you for taking the time to explain, much appriciated!

Reply

You must log in to post.

About this Topic