Search code examples
phpwordpresspermalinks

Wordpress 'post_type_link' hides permalink


So I just added a custom post type through the WP-Types plugin, nothing fancy just a custom post type. Now the permalink of this custom post type isn't showing up.

I figured out that it had something to do with the rewriting of my (Woocommerce) products permalinks with this code:

function append_id_string( $link, $post ) {
    $post_meta = $post->ID;
    if ( 'product' == get_post_type( $post ) ) {
        $link = $link . '#' .$post_meta;
        return $link;
    }
}

add_filter( 'post_type_link', 'append_id_string', 1, 2 );

After removing this piece of code the permalink shows up.

Why is the above code also affecting my custom post type and how can I use this code to only affect my (Woocommerce) products


Solution

  • This is my guess but I think you should return the $link variable whether the condition is met or not. Like this:

    function append_id_string( $link, $post ) {
        $post_meta = $post->ID;
        if ( 'product' == get_post_type( $post ) ) {
            $link = $link . '#' .$post_meta;
        } 
        return $link;
    }
    
    add_filter( 'post_type_link', 'append_id_string', 1, 2 );