• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Category Template Sort Order

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › Design Tips and Tricks › Category Template Sort Order

This topic is: not resolved

Tagged: pre_get_posts, random order

  • This topic has 8 replies, 2 voices, and was last updated 9 years ago by deejuk.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • May 30, 2017 at 9:39 am #207156
    deejuk
    Member

    Hi all.

    Can anyone please tell me what piece of code I need to add to a category template to sort the posts in a random order.

    All I have in my current category template is :-

    <?php
    /**
    * Genesis Framework.
    *
    * WARNING: This file is part of the core Genesis Framework. DO NOT edit this file under any circumstances.
    * Please do all modifications in the form of a child theme.
    *
    * @package Genesis\Templates
    * @author StudioPress
    * @license GPL-2.0+
    * @link http://my.studiopress.com/themes/genesis/
    */

    //* Move featured image above post title in archives
    remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 );
    add_action( 'genesis_entry_header', 'genesis_do_post_image', 8 );

    genesis();


    WN6 Web Design – North West of England

    http://local
    May 30, 2017 at 10:39 pm #207194
    Brad Dalton
    Participant

    Use pre_get_posts

    With the rand parameter


    Tutorials for StudioPress Themes.

    May 31, 2017 at 3:27 am #207199
    deejuk
    Member

    Hi Brad thanks for that but how do I write it into the function for the category template


    WN6 Web Design – North West of England

    May 31, 2017 at 3:46 am #207200
    Brad Dalton
    Participant

    Use a conditional tag


    Tutorials for StudioPress Themes.

    May 31, 2017 at 5:24 am #207204
    deejuk
    Member

    Hi Brad thanks again .. sorry I'm no good at code. I'll leave it thanks for your pointers


    WN6 Web Design – North West of England

    May 31, 2017 at 5:45 am #207207
    Brad Dalton
    Participant

    deejuk

    If you search for pre_get_posts you'll find code like this

    You can then add a conditional tag after the function to control where the code executes.

    Some effort is required at your end..


    Tutorials for StudioPress Themes.

    May 31, 2017 at 7:29 am #207216
    deejuk
    Member

    Hi ... thanks for the lead on this I have worked it out now with your help.

    I tired the following code in my category-4.php template

    add_action( 'pre_get_posts', 'generate_random_category_posts');
    function generate_random_category_posts( $query ) {
    if ( $query->is_category()) {
    $query->set( 'orderby', 'rand' );
    }
    }

    It didn't work ...

    So I put this in my functions.php

    add_action( 'pre_get_posts', 'generate_random_category_posts');
    function generate_random_category_posts( $query ) {
    if ( $query->is_category(4)) {
    $query->set( 'orderby', 'rand' );
    }
    }

    And it worked. Plus the blog area remains sorted chronologoically.

    Can't work out why it didn't work in category-4.php but at least it's working now ... really need to get my head around all of this but thank you Brad once again


    WN6 Web Design – North West of England

    May 31, 2017 at 7:49 am #207217
    Brad Dalton
    Participant

    This is what i would use:

    add_action( 'pre_get_posts', 'generate_random_category_posts');
    
    function generate_random_category_posts( $query ) {
    
        if ( $query->is_main_query() && !is_admin() && $query->is_category('4') ) {
        
        $query->set( 'orderby', 'rand' );
        
        }
    }   
    

    Tutorials for StudioPress Themes.

    May 31, 2017 at 11:36 am #207228
    deejuk
    Member

    Hi Brad - you're smarter than me for certain.. I need to study more


    WN6 Web Design – North West of England

  • Author
    Posts
Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble