0

I spent most of the day reviewing past questions and answers and have not found anything that addresses what I am trying to do. Maybe I didn't word my search correctly.

Lets say I have a category of posts for documents that reflect current committee members. I can create a WP menu item for a specific post with Wordpress However, users can submit new posts using the USP Pro plugin. Now the menu item needs to point to the most recently updated post. I want to avoid having to update the menu every time a new post is added.

I created a short code function in my functions.php file to get the most recent post for a category (short code argument). When I attempt to redirect to the permalink for that post, I get a blank page.

    //[latestpost]
function get_latest_post( $atts) { extract( shortcode_atts( array( 'cat' => '', ), $atts, 'latestpost' ) ); $args = array( 'posts_per_page' => 1, // we need only the latest post, so get that post $ 'cat' => $cat, // Use the category id, can also replace with category_nam$ //'category_name' => 'SLUG OF FOO CATEGORY, ); $q = new WP_Query( $args); if ( $q->have_posts() ) { while ( $q->have_posts() ) { $q->the_post(); //Your template tags and markup like: //the_title(); $link=get_permalink($post); // echo $link; // echo $cat; if ( wp_redirect( $link) ) { exit; } } wp_reset_postdata(); } } add_shortcode('latestpost','get_latest_post')`

 

I put in a couple of echo statements to confirm that the function was getting the correct category ($cat) and permalink. They appear on the page but the page is not redirected.

Running Wordpress 5.1 with a twenty seventeen child theme.

Thanks for your help.

  • Why not display a clickable link instead of manually redirecting the user? After all, the redirect will fail if there has been an output (e.g. space, text, HTML) sent to the browser. And although this isn't the problem, $post is undefined in your shortcode function. – Sally CJ Feb 26 at 3:45
  • Thank you for your response. I did not realize I could not redirect at that point. With respect to making it a clickable link, since users with upload rights are using USP Pro, not the admin interface, the link has to be updated to point to the latest upload. Which is the nut of the problem. My guess is that I will have to create a plugin to update the menu item to point to the most recent post. – Joe Mohr Feb 27 at 16:22
0

If you want to perform any redirects, you have to send your header before site sends any output.

Shortcodes are run when the content is obtained, parsed and printed. So the site will already be partially sent to the browser, so you won't be able to perform any redirects any more.

So you should run your code using some hook that is fired before any output is sent to the browser. You can use wp hook for example.

And if you really want the shortcode to cause redirection, then:

  1. Display a link instead - it will be less confusing for users, I guess.
  2. You'll have to do the redirection in JS:

.

function get_latest_post( $atts) {
    extract( shortcode_atts( array(
        'cat' => '',
    ), $atts, 'latestpost' ) );

    $args = array(
        'posts_per_page' => 1, // we need only the latest post, so get that post $
        'cat' => $cat, // Use the category id, can also replace with category_nam$
        //'category_name' => 'SLUG OF FOO CATEGORY,
    );
    $q = new WP_Query( $args);
    if ( $q->have_posts() ) {
        while ( $q->have_posts() ) {
            $q->the_post();

            // Your template tags and markup like:
            // the_title();
            $link=get_permalink($post);
            // echo $link;
            // echo $cat;
            echo '<script type="text/javascript>document.location.href = "' . $link . '";</script>'
            return;  // you want only one redirection in your site
        }
        wp_reset_postdata();
    }
}
add_shortcode( 'latestpost', 'get_latest_post' );
0

I have a solution mostly completed. I created a plugin based on Recent Post to WP Nav Menu by Ankit. That plugin displays the most recent for a Post Type. It is over a year old and not tested with the current version of WordPress. There is only one review. I modified the code to use Categories rather than Post Type. The only issue I am struggling with at the moment is that when the nav-menu-item is created, the 'object' is set to "category" which potentially conflicts with the standard option of creating a menu item based on category. Need to figure out how to change the object type. If I can figure that out, I will make the plugin available to others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.