Skip to content

Instantly share code, notes, and snippets.

@arioch1984
Created May 22, 2014 12:45
Show Gist options
  • Save arioch1984/a33fe03310cdd7dbc85c to your computer and use it in GitHub Desktop.
Save arioch1984/a33fe03310cdd7dbc85c to your computer and use it in GitHub Desktop.
WP remove filter after first usage
//Taken from http://stackoverflow.com/questions/18206115/how-to-avoid-affecting-other-queries-when-using-posts-orderby
// My list of post IDs in my custom order
$my_post_ids = array(1,3,2);
// Apply filter to the ORDERBY SQL statement
add_filter('posts_orderby', 'my_custom_orderby');
function my_custom_orderby($orderby_statement) {
// Disable this filter for future queries!
remove_filter(current_filter(), __FUNCTION__);
global $my_post_ids;
$orderby_statement = 'FIELD(ID, '.implode(',',$my_post_ids).')';
return $orderby_statement;
}
// My custom query
$my_custom_query = new WP_Query(array('post_type' => 'post', 'post__in' => $my_post_ids);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment