Make a specific post to appear first (sticky) in a WP_Query

August 29, 2013

function hint_make_sticky($postID,$wp_query){
 global $hint_sticky_ID;
 global $hint_posts;
 global $post;

$hint_posts = array();
 $hint_sticky_ID = $postID;

if ($wp_query->have_posts()){
 while($wp_query->have_posts()){
 $wp_query->the_post();
 array_push($hint_posts,$post->ID);
 }
 }

$hint_sticky_pos = array_search($hint_sticky_ID,$hint_posts);
 hint_move_value($wp_query->posts, $hint_sticky_pos, 0);

}

hint_move_value(&$array, $a, $b) {
 $out = array_splice($array, $a, 1);
 array_splice($array, $b, 0, $out);
}

Tags: ,

Leave a Comment