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.

4
  • 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, 2019 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, 2019 at 16:22
  • could you not create a redirect from a falepage eg mysite.com/falsehome and then redirect that to an ALL category or ALL tag in some way i have a complex site home page that doesn't work well on mobile where i want to give a mobile menu option to display all posts ie latest posts. havent quite figured it out but redirecting to a specific category can sole other problems
    – Mark Gee
    Mar 19, 2020 at 21:18
  • ok the answer is to use multiple categoris in the url ewg like this mysiteDOTcom/category/Late,opinion,news,uncategorized/ you can choose all of your most common categories...it's a 98% solution. and it requires zero complex coding
    – Mark Gee
    Mar 20, 2020 at 23:04

1 Answer 1

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' );
1
  • tnx plz send code show post wp only mobile Do not show the post in the mobile version in the desktop version link me fontjo Dec 29, 2020 at 18:12

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