Do I need some reset functionality?
Yes, you do — when you call $the_query->the_post()
, the global $post
variable will be modified so that the the_
functions like the_title()
, the_content()
and the_permalink()
will use the current post in your custom loop, and thus after the loop ends, you should call wp_reset_postdata()
in order to restore the $post
global to the current post in the main query (that’s referenced via the global $wp_query
variable), because otherwise then the issue in question would happen.
And your code/function is actually calling wp_reset_postdata()
, except that it never got called because it’s called after the return
line, i.e. you did return $string; wp_reset_postdata();
..
So you would just need to swap their position, like so:
wp_reset_postdata(); // call this first
return $string; // then return the output
But I would probably call the function right after the while
block ends, i.e. while ( ... ) { ... } wp_reset_postdata();
, because there’s nothing to reset if the $the_query->the_post()
didn’t run.
Additionally, the $post_id
as in get_the_post_thumbnail($post_id, array( 50, 50) )
is never defined in your code, so you should change that to get_the_ID()
, or use null
instead to use the current post in your loop.