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

  1. Yake
    Posted 2 years ago #


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

    It looks like this

    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?


  2. bcworkz
    Posted 2 years ago #

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

  3. Yake
    Posted 2 years ago #

    Thank you! Works like a charm.

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


  4. bcworkz
    Posted 2 years 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
    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic