How to avoid affecting other queries when using posts_orderby?
This filter takes two parameters, $orderby
and &$this
. "this" being the WP_Query object. I'm not sure how to detect that WPML is making the call, but we can check that your call is the one beingmade.
$my_post_ids = array(1,3,2);add_filter( 'posts_orderby', 'my_custom_orderby', 10, 2 );function my_custom_orderby( $orderby_statement, $object ) { global $my_post_ids; if( $my_post_ids != $object->query['post__in'] ) return $orderby_statement; // Disable this filter for future queries! remove_filter( current_filter(), __FUNCTION__ ); $orderby_statement = 'FIELD(ID, ' . implode( ',', $my_post_ids ) . ')'; return $orderby_statement;}