Created
May 22, 2014 12:45
-
-
Save arioch1984/a33fe03310cdd7dbc85c to your computer and use it in GitHub Desktop.
WP remove filter after first usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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