You can simply use var_dump()
to do this. That is how I check values inside functions and filters.
I have the following line of code in a file which I simply copy and paste where needed to dump the value of a variable
?><pre><?php var_dump( $variable_to_test ); ?></pre><?php
The pre
tags dump a nice readable array/object/string depending on the value. Inside your function you can just do
?><pre><?php var_dump($ad_posts); ?></pre><?php
after ksort( $ad_posts );
.
Just make sure to call the function somewhere if it is not hooked to some kind of hook otherwise nothing will happen