Sorting your posts by relevance. The code below will output the posts ordered by relevance I have added some comments for some clarification let me know if it works.
function my_custom_query(){
// get only the posts without order them
$my_query_posts = new WP_Query( array( 'numberposts' => 5 ) );
// check relevance posts
foreach( $my_query_posts->posts as $post )
$post->relevance = check_relevance( $post );
// sorting the posts
usort( $my_query_posts->posts, 'compare' );
return $my_query_posts;
}
function check_relevance( $post ){
// check and calculate the relevance post
return rand( 0, 100 );
}
function my_compare( $a, $b ){
if( $a->relevance == $b->relevance )
return 0;
return ( $a->relevance > $b->relevance ) ? -1 : 1;
}
$posts = my_custom_query();
// output posts
while( $posts->have_posts() ){
$posts->the_post();
}