• Hi there,

    I’m trying to change the default post order on an archive page but having no luck. I notice a similar question has already been asked here, and the response was to use WP_Query. Just to be clear, I’m not trying to sort posts using the FE sort widget, I’m trying to simply change the default order posts appear when the page is loaded.

    When using the standard loop on an archive page (as described in the WP_Query documentation), I notice the filters no longer work which leads me to believe the basic loop overrides the arguments that get added when a user clicks on a filter?

    What is the correct way to ensure a custom loop changes the order of posts, but also respect the filter everything arguments?

Viewing 1 replies (of 1 total)
  • Thread Starter Dan204

    (@dan204)

    Just to follow up on this, I’ve stumbled across a way to order posts outside of the loop which so far appears to have worked, for anyone else who’s interested the code I’ve added to my functions file is:

    add_filter("posts_orderby", "vmf_orderby_filter", 10, 2);
     
    function vmf_orderby_filter($orderby, &$query){
        global $wpdb;
        //figure out whether you want to change the order
        if (get_query_var("post_type") == "courses") {
             return "$wpdb->posts.post_title ASC"; //change post_title and ASC to suit using WP query parameters
        }
        return $orderby;
     }

    The original code and explanation can be found here.

    I would still be interested to know what your recommendation would be to achieve this sort of ordering using the standard loop, but for now this appears to have gotten me out of trouble.

Viewing 1 replies (of 1 total)
  • The topic ‘Using WP loop breaks filtering’ is closed to new replies.