[Resolved] Control query sort order on recent posts footer widget

Home Forums Support [Resolved] Control query sort order on recent posts footer widget

Home Forums Support Control query sort order on recent posts footer widget

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #471843
    Denise

    Hi GP Friends,
    I have a widget in the footer that shows the most recent posts. I also have some code in the functions.php that controls the sort order for certain categories. Instead of using the default order by date descending, it sorts certain categories by Title. I noticed that this is affecting the sort order of the footer widget for recent posts. If the user is on a category archive page that sorts by title, then the recent posts footer also sorts by title. Can you suggest some code for how I might control the footer widget sort order separately from the category archive query?

    Many thanks,
    Denise

    Here is the sort code I have in the functions.php:

    /* Change query to sort by title for selected categories */
    add_action( 'pre_get_posts', 'mdp_modify_query_order' );
    function mdp_modify_query_order( $query ) {
        $cat_title = single_cat_title("",false);
        if ( is_search() || is_category( array('robots','websites','organizations','journals','blogs','collections')) || stripos( $cat_title,'robot') > 0) {
            $query->set( 'orderby', 'title' );
            $query->set( 'order', 'ASC' );
        }
    }
    GeneratePress 2.0.1
    GP Premium 1.5.6
    #472411
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    You should be able to check if it’s the main query or not in your conditional:

    add_action( 'pre_get_posts', 'mdp_modify_query_order' );
    function mdp_modify_query_order( $query ) {
        $cat_title = single_cat_title("",false);
        if ( ! $query->is_main_query() && ( is_search() || is_category( array('robots','websites','organizations','journals','blogs','collections')) || stripos( $cat_title,'robot') > 0 ) ) {
            $query->set( 'orderby', 'title' );
            $query->set( 'order', 'ASC' );
        }
    }
    #475768
    Denise

    Thanks so much, Tom. That did the trick.

    P.S.
    As you have rightly heard so often, but it can’t be said too many times, your Generate Press theme is absolutely wonderful. This is exactly the tool I needed to build a lightweight resource directory. I searched everywhere and tried many different themes and plug-ins, including starting with underscores, which was just a bit too much. I’m a database developer and unfortunately, don’t have time to do all the work needed to build and test a theme from scratch. Generate Press gives me just the right amount of control. It’s well tested with fantastic support. A million thanks, once again. You’ve made an incredible contribution to the WordPress Community.

    Best wishes,
    Denise

    #476064
    Tom
    Lead Developer
    Lead Developer

    Wow, thank you so much Denise! I really appreciate the kind words 🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.