0

I found some code online for creating a custom permalink for a custom post type:

add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3);

    function wpse33551_post_type_link( $link, $post = 0 ){
        $timestamp = $post->post_date;
        $timestamp = preg_replace("/[^A-Za-z0-9]/", "", $timestamp);
        if ( $post->post_type == 'recipe' ){
            return home_url( 'recipe/' . $timestamp );
        } else {
            return $link;
        }
    }

    add_action( 'init', 'wpse33551_rewrites_init' );

    function wpse33551_rewrites_init(){
        add_rewrite_rule(
            'recipe/([0-9]+)?$',
            'index.php?post_type=recipe&p=$matches[1]',
            'top' );
    }

In the wordpress admin the correct url is displayed, but upon clicking the preview post button it returns a 404. I have tried flushing rewrite rules but to no avail.

Any ideas?

0

The problem with the code you've posted is that you're using the timestamp to create the permalink, and using it in add_rewrite_rule as if the timestamp was the post ID of the recipe. The p-parameter in index.php?post_type=recipe&p=$matches[1] is used for passing post ID's. Thus, WordPress will start looking for a post with an ID like "20140609070153".

Now I'm not sure why you would want a permalink such as this, but to keep a similar permalink structure that actually works, you could add the ID to the permalink structure, creating, for example, the permalink http://example.com/recipe/21-20140609070153, which is passing the post ID (21) as well.

add_filter( 'post_type_link', 'wpse33551_post_type_link', 1, 3 );

function wpse33551_post_type_link( $link, $post = 0 ) {
    $timestamp = $post->post_date;
    $timestamp = preg_replace("/[^A-Za-z0-9]/", "", $timestamp);

    if ( $post->post_type == 'recipe' ) {
        return home_url( 'recipe/' . $post->ID . '-' . $timestamp );
    }
    else {
        return $link;
    }
}

add_action( 'init', 'wpse33551_rewrites_init' );

function wpse33551_rewrites_init() {
    add_rewrite_rule(
        'recipe/([0-9]+)\-([0-9]+)?$',
        'index.php?post_type=recipe&p=$matches[1]',
        'top'
    );
}

A problem with this solution is that it would yield a valid page for any timestamp appended.

Concluding: I would move to another permalink structure, actually uniquely identifying a post (which a timestamp doesn't) :-).

  • Thanks for this - adding the ID makes a lot of sense as then it does make it a unique number. I'm using timestamp as I don't want the URL easily guessable by type in traffic. Using your code shows the correct URL but I'm still getting a 404. Open to any suggestions of a better way of doing this. – user319940 Jun 9 '14 at 8:43
  • If you don't want it to be guessable, you might want to look into another method of doing so, for example by choosing a more "advanced" rewrite structure with, for example, a unique custom field. Did you flush the rewrite rules by going to Settings -> Permalinks? – engelen Jun 9 '14 at 8:46
  • Yep - tried flushing. Still a 404 unfortunately. – user319940 Jun 9 '14 at 8:47

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.