5

I use a WP template to generate an email newsletter. I display future posts and until recently using the_permalink displayed the full human-readable URL (e.g. /this-is-a-page). Now it displays the post ID URL (e.g. ?p=5800). How do I get WP to display the human-readable URL?

More strange, perhaps, on the Add/Edit page for my future entries, when I add or update a story to be published in the future, the message up top "Post updated. View post" link goes to the post ID URL. However, the View Post button to the right of the Permalink, located directly under the entry title input field, goes to the human-readable URL.

It's a major PITA and totally counter-productive to manually convert the post URLs to human readable URLs. I need the HTML output from my template to set up HTML emails before the stories are published.

Any ideas how to retrieve and display human-readable URLs for future stories?

Thanks for any help and insights.

3 Answers 3

2

Permalink settings are available under Permalinks under the settings menu.

I have faced a similar issue resetting the options here to non rewritten URLs and then back to the rewritten URLs should reset any issues with .htaccess file

3
  • Sorry to not be entirely clear: I had already set permalinks then clicked to be sure the option held. My site has been up for two years and all the pretty link URLs work for published stories. I have a problem only with the URLs for stories to be published in the future. This problem started some time between late January and late March 2015. Something changed in WP files to "break" the_permalink and get_permalink for future pretty links.
    – FredHead
    Jul 20, 2015 at 0:56
  • 1
    Hi I found this on the WordPress Support Forum. wordpress.org/support/topic/… Jul 22, 2015 at 4:25
  • You found the magic bean, Alexander, thank you! Something must have changed in core earlier this year. And I was looking for the right filter to use to convert an ID URL to a pretty URL, which this link provides. I appreciate your persistence and help!
    – FredHead
    Jul 22, 2015 at 14:09
3

Just came across this same issue.

A wordpress moderator recommended using the post_link hook if you don't mind people knowing your future permalinks. This needs to be added in your functions.php file


functions.php

add_filter( 'post_link', 'future_permalink', 10, 3 );

function future_permalink( $permalink, $post, $leavename ) {
    /* for filter recursion (infinite loop) */
    static $recursing = false;

    if ( empty( $post->ID ) ) {
        return $permalink;
    }

    if ( !$recursing ) {
        if ( isset( $post->post_status ) && ( 'future' === $post->post_status ) ) {
            // set the post status to publish to get the 'publish' permalink
            $post->post_status = 'publish';
            $recursing = true;
            return get_permalink( $post, $leavename ) ;
        }
    }

    $recursing = false;
    return $permalink;
}

Thanks to Alexander Gounder in the comments of this page for finding the solution. I've added it here for clarity.

1
  • 1
    This is useful but it doesn't restore the old value of post_status when it's done. If any subsequent check is done to $post->post_status it's going to think it's published, not future
    – Don
    Apr 23, 2018 at 16:32
0

Thanks so much for your helpful solution, Adam! Today I stumbled upon exactly the same issue that @Don is mentioning in his little comment:

This is useful but it doesn't restore the old value of post_status when it's done. If any subsequent check is done to $post->post_status it's going to think it's published, not future

Which is actually an issue, as it might make future posts (or worse, drafts if you change the function to handle these as well!) available to the public in a context where you might not want it.

But there is a simple solution to this problem. clone the $post before returning the permalink, like so:

add_filter( 'post_link', 'future_permalink', 10, 3 );

function future_permalink( $permalink, $post, $leavename ) {
    /* for filter recursion (infinite loop) */
    static $recursing = false;

    if ( empty( $post->ID ) ) {
        return $permalink;
    }

    if ( !$recursing ) {
        if ( isset( $post->post_status ) && ( 'future' === $post->post_status ) ) {
            // clone the post before changing it's status
            $clone = clone $post;
            // set the post status to publish to get the 'publish' permalink
            $clone->post_status = 'publish';
            $recursing = true;
            return get_permalink( $clone, $leavename ) ;
        }
    }

    $recursing = false;
    return $permalink;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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