wpml - How to avoid affecting other queries when using posts_orderby? -


in wordpress must known, when using get_posts() or query_posts() or wp_query, not possible order returned posts specifying list of post id in order want.

instead have loop through results , re-order them on php side. performance hit , bad practice. instead should use built-in mysql functions retrieve posts in desired order upfront.

thankfully there posts_orderby can used specify custom orderby statement, this:

// list of post ids in custom order $my_post_ids = array(1,3,2);  // apply filter orderby sql statement add_filter('posts_orderby', 'my_custom_orderby'); function my_custom_orderby($orderby_statement) {     global $my_post_ids;     $orderby_statement = 'field(id, '.implode(',',$my_post_ids).')';          return $orderby_statement; }  // custom query $my_custom_query = new wp_query(array('post_type' => 'post', 'post__in' => $my_post_ids); 

however there problem above code, affect order of all queries on page! including queries made plugins, shortcodes, , on.

easy fix!

the simple way fix this, apply filter 1 time, , remove called, putting remove_filter() within filter itself, run once:

// list of post ids in custom order $my_post_ids = array(1,3,2);  // apply filter orderby sql statement add_filter('posts_orderby', 'my_custom_orderby'); function my_custom_orderby($orderby_statement) {      // disable filter future queries!     remove_filter(current_filter(), __function__);      global $my_post_ids;     $orderby_statement = 'field(id, '.implode(',',$my_post_ids).')';          return $orderby_statement; }  // custom query $my_custom_query = new wp_query(array('post_type' => 'post', 'post__in' => $my_post_ids); 

because set filter before custom query, once execute custom query should filtered posts_orderby filter set above, disabled won't affect future queries.

in theory, that's great, , works great in case scenarios!

an issue wpml

however have encountered case when using wpml plugin filter affects other queries mine , causes errors. believe wpml plugin creating query of own executed before own custom query, making filter applies wpml query instead of mine!

is there possible way add check within filter make sure affects correct query?

thank much


edit:

the fix wpml

for information, while accepted answer question correct, didn't solve problem having wpml. here how fixed wpml conflict:

// list of post ids in custom order $my_post_ids = array(1,3,2);  // apply filter orderby sql statement add_filter('posts_orderby', 'my_custom_orderby'); function my_custom_orderby($orderby_statement) {      // disable filter future queries!     remove_filter(current_filter(), __function__);      global $my_post_ids, $wpdb;     $orderby_statement = 'field('.$wpdb->base_prefix.'posts.id, '.implode(',',$my_post_ids).')';          return $orderby_statement; }  // custom query $my_custom_query = new wp_query(array('post_type' => 'post', 'post__in' => $my_post_ids); 

this filter takes 2 parameters, $orderby , &$this. "this" being wp_query object. i'm not sure how detect wpml making call, can check your call 1 being made.

$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 filter future queries!     remove_filter( current_filter(), __function__ );      $orderby_statement = 'field(id, ' . implode( ',', $my_post_ids ) . ')';          return $orderby_statement; } 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -