admin管理员组

文章数量:1122832

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.

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.

Share Improve this question asked Jul 19, 2015 at 4:09 FredHeadFredHead 3101 gold badge4 silver badges10 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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

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.

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;
}

本文标签: permalinksthepermalink displays post ID URL for future posts