Skip Navigation

[Resolved] Random order in archive view

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user would like to have a random order for posts in a taxonomy archive.

Solution:
The archive query is not built using Toolset, we'll need custom code in order to change the order on it.

add_filter('pre_get_posts', 'order_archive_by_title');
function order_archive_by_title($query) {
    if ( $query->is_tax( 'practice-area' ) ) {
        $query->set( 'orderby', 'rand' );
    }
    return $query;
}

Relevant Documentation:
https://developer.wordpress.org/reference/hooks/pre_get_posts/

This support ticket is created 3 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by matthewW-12 3 years, 6 months ago.

Assisted by: Jamal.

Author
Posts
#1817703

They have done, yes. I just thought Toolset might offer a way to include php with a content template (so in effect creating a condition), but as I say, it's not crucial.

Can I ask another question on the same site while we're talking about it? When displaying lists of custom posts according to a taxonomy term, like the one we looked at before:
hidden link
is it possible to get the posts to display in random order?

#1817709

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Currently, we can't set the random order for an archive template through the user interface, but we can do it with custom code. Check this previous reply, it has an example code, that you'll need to adapt to your taxonomy slug:
https://toolset.com/forums/topic/how-to-display-archive-view-in-random-order/#post-146962

The following code should work for your taxonomy(practice-area)

add_filter('pre_get_posts', 'order_archive_by_title');
function order_archive_by_title($query) {
    if ( $query->is_tax( 'practice-area' ) ) {
        $query->set( 'orderby', 'rand' );
    }
    return $query;
}
#1817783

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.