• candell

    (@candell)


    I want to update the main query and taxonomy queries so they show the results randomly

    function func_custom_order( $qry ) {
        if ( $qry->is_main_query() && $qry->is_tax() ) {
            $qry->set( 'orderby', 'rand' );
        }
    }
    add_action( 'pre_get_posts', 'func_custom_order' );

    Without this in functions.php, the ordering is;

    ORDER BY wp_posts.menu_order, wpsc_posts.post_date DESC

    With this;

    ORDER BY wp_posts.menu_order, RAND()

    Can I have it just order by rand, and not also menu_order?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Joy

    (@joyously)

    There might be another filter for the SQL itself, but using random is a bad user experience because the pagination means you might never see everything. It would be chaos.

    Moderator bcworkz

    (@bcworkz)

    Yes, you cannot paginate random queries. Something else is modifying the orderby, try passing a large priority argument when you add to the action hook. If all else fails, you can modify the SQL through the “posts_orderby” filter.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    In addition, if you do any caching, it’s unlikely any viewer will see a different ordering on a page refresh.

    Thread Starter candell

    (@candell)

    Thanks folks, I have caching turned off on those pages. What would you suggest for the ‘large priority argument’?

    Moderator bcworkz

    (@bcworkz)

    This should be adequate:
    add_action( 'pre_get_posts', 'func_custom_order', 999 );

    You can go to the extreme if you want:
    add_action( 'pre_get_posts', 'func_custom_order', PHP_INT_MAX );

    Thread Starter candell

    (@candell)

    I found it, they had installed a plugin called “Post Types Order” to allow them to drag and drop posts. Setting the priority allows the posts to be random on all pages but the page they want to manually order via that plugin.

    Thanks for the help

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Order by rand only’ is closed to new replies.