it looks like the problem is caused by the “Post Types Order” plugin I
have installed
Yes, I believe so because your code actually worked for me, but after I installed and activated the plugin, your code no longer worked as expected.
I have it set to not order
news
post types, but for some reason it’s
still adding its sorting to the main query.
I don’t know how you set it, but try one of these:
-
Set
ignore_custom_sort
(it’s a customWP_Query
parameter) totrue
like so:switch ( $order_by ) { case 'news-date': $query->set( 'meta_key', 'date' ); $query->set( 'orderby', 'meta_value_num' ); $query->set( 'ignore_custom_sort', true ); break; }
-
Or you can also use the
pto/posts_orderby/ignore
hook to disable the plugin’s sorting:add_filter( 'pto/posts_orderby/ignore', function( $ignore, $order_by, $query ){ if ( 'lesson' === $query->get( 'post_type' ) ) { $ignore = true; } return $ignore; }, 10, 3 );