FacetWP orderby not working

Hey Jenn, I’m far from a backend dev and I haven’t really worked with PHP, so I’m going to do my best to help here but I’m a little underqualified!

The documentation from FacetWP about customizing WordPress archive queries highlights the importance of using conditional tags to specify when the query should be changed. I noticed that you seem to be using is_main_query() by itself as your conditional tag, which “Returns true when the current query is the ‘main’ query.” If I’m understanding the docs correctly, that would mean that with your PHP snippet, you’d be changing the query every time a query exists. Hopefully a more experienced dev could fact-check me on this, but if my understanding is correct, then your logic could cause some issues for you down the road since you might be modifying some queries you don’t intend to. I bugged @julia and she sent me this which might work better for you to replace the second line of your snippet:

if ( $query->is_post_type_archive( 'sfwd-courses' ) && $query->is_main_query() && !is_admin() ) {

Another thing that might be causing you issues is that you have {Field post_class} written in your outer div. I’d usually expect to see the post classes added to a div within the loop so that it could grab the post classes from those loop items, such as in <div class="post-wrapper {Field post_class}">. This probably depends on your data structure and what you’re trying to achieve, but I thought I’d point that out since it might be causing some issues for you.

Regarding your comment “I have tried adding orderby=title order=ASC here, but it didn’t do anything,” this is because when you don’t specify a loop type, the query parameters on that loop tag are ignored and it just uses the default loop of the page/archive you’re on. This means that <Loop orderby=title> and <Loop> would display the same result because when no type is specified, it uses the query parameters from the default loop.

One last thing I’m noticing is that the example in the L&L docs uses add_action() instead of add_filter(). My knowledge of PHP is too limited to know whether there’s an important difference there (I tried doing some googling but I’m still a little lost) but I wonder if you might try swapping that part out for something like this:

function fwp_home_custom_query( $query ) {
    if ( $query->is_post_type_archive( 'sfwd-courses' ) && $query->is_main_query() && !is_admin() ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'fwp_home_custom_query' );

Want to give those suggestions a try and see if that fixes/changes anything for you?