FacetWP orderby not working

I’m using FacetWP on an archive. I’ve set it to just per the documentation. When I call out the Loop with a specific type, it orders by title, which is what I want. But when I put just loop it orders by publish date…

I’ve tried fixing this by adding orderby=title to the Loop and by adding the code below to the functions.php file, but neither is working. Am I doing something wrong?

function fwp_home_custom_query( $query ) {
    if ( $query->is_main_query() ) {
        $query->set( 'post_type', 'sfwd-courses' );
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
}
add_filter( 'pre_get_posts', 'fwp_home_custom_query' );

I removed FacetWP from the equation and got the same results. So Loop without the type defined isn’t recognizing term order…

Also this is the Loop I’m using

<div class="course-grid facetwp-template {Field post_class}">
<Loop (I have tried adding orderby=title order=ASC here, but it didn't do anything)> 
<div class="fl-post-grid-post"> 
  <div class="post-wrapper">
  <div class="fl-post-text">
    <Shortcode course_notstarted course_id="{Field id}"><div class="enrolled-flag">Enrolled</div></Shortcode>
    <Shortcode course_inprogress course_id="{Field id}"><div class="inprogress-flag">In Progress</div></Shortcode>
    <Shortcode course_complete course_id="{Field id}"><div class="completed-flag">Completed</div></Shortcode>
    <div class="grid-title">
    <div class="course-icon"><Field acf_oembed=course_svg_icon /></div>
      <h2 class="fl-post-title"><a href="{Field url}"><Field title /></a></h2>
    </div>
    <div class="full-button"><a href="{Field url}">View Course</a></div>
  </div>
  </div>
</div>
</Loop>
</div>

I’m not a pro, and I’m not sure why you are creating a loop in PHP, but FWIW, here is a loop I use

<Loop type="goods" paged="{Get local=vpage}" sort_field=published_date sort_order=desc scroll_top=true>

And here is a category loop

<Loop type="quotes" taxonomy=quote_category terms="Front" count=8 orderby=random>

Maybe that will help. If not, the pros on this forum are great about feedback.

Cheers, Richard

Thanks, Richard. Unfortunately when using FacetWP to filter you can only use the main query so it has to be just <Loop>.

You can find more info here: Using Loops with FacetWP - Tangible Inc. Knowledge Base

And here is FacetWP’s documentation on altering the query: How to Customize WordPress Archive Queries | FacetWP

Hi Jenn,
I am new to this too but you can maybe try to write order=asc instead of order= ASC in your code. Cuz I tried this in one of my template and it made a difference.

Yep! Tried that as well

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?

That didn’t work either. I tried disabling all the plugins too and still had the same issue… so maybe it is something with LearnDash? I tried reaching out to them, but that was super unhelpful LOL

Sorry to hear that my suggestions didn’t solve your issue! I’m not sure if LearnDash is actually the culprit, but the fact that your template is displaying everything you need (just in the wrong order) indicates that the L&L markup you’ve written is working perfectly and any remaining troubleshooting needs to happen on the PHP/FacetWP/LearnDash side of things to modify your query. You might try posting in the LearnDash Facebook group in case that’s more helpful than reaching out to LD directly for support. Beyond that, FacetWP support might be able to give you a hand. When you do find a solution to modify your query, be sure to post an update here!

Good luck :smiley:

A friend of mine helped me troubleshoot this and the issue was with FacetWP they had it set so their update query vars run at 999. Code that ended up working:

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

Thanks everyone for your input!!

2 Likes