In 90% of the cases, the_permalink function is not working because it’s not in The Loop. Additionally, you can pass the post ID or the $post object to retrieve the proper URL when outside of the loop.