shortcode – menu item to display the most recent message

I spent most of the day reviewing the previous questions and answers and I found nothing that responded to what I was trying to do. Maybe I did not formulate my research correctly.

Let's say I have a category of messages for documents that reflect current committee members. I can create a WP menu item for a specific message with WordPress. However, users can submit new messages using the USP Pro plug-in. Now the menu item should point to the last updated post. I want to avoid having to update the menu each time a new message is added.

I have created a short code function in my functions.php file to get the most recent message of a category (short code argument). When I try to redirect the permanent link to this message, I receive a blank page.

                //[latestpost]
function get_latest_post ($ atts) { extract (shortcode_atts (array ( & # 39; Chat & # 39; => & # 39; ), $ atts, & # 39; latestpost & # 39;)); $ args = array ( & # 39; posts_per_page & # 39; => 1, // we only need the last post, so get this post $ & # 39; cat & # 39; => $ cat, // Uses the category identifier, can also replace by category_nam $ // category_name & # 39; => SLUG OF FOO CATEGORY, ) $ q = new WP_Query ($ args); if ($ q-> have_posts ()) { while ($ q-> have_posts ()) { $ q-> the_post (); // Your template tags and tags look like this: //the title(); $ link = get_permalink ($ post); // echo $ link; // echo $ cat; if (wp_redirect ($ link)) { exit; } } wp_reset_postdata (); } } add_shortcode (& # 39; latestpost & # 39 ;, & gt; get_latest_post & # 39;)

I put some echo statements to confirm that the function was getting the right category ($ cat) and a permanent link. They appear on the page but the page is not redirected.

Running WordPress 5.1 with a theme of seventeen children.

Thank you for your help.