Jack Ottermans
Jack Ottermans

Reputation: 139

WordPress Custom Type append Post ID at the end

I've been searching for two days know and tried almost every example I've seen online but the real problem is that all examples have different post-type-names or want different results in general.

That's why I want to ask for help and start from scratch.

I've created a Custom Post Type for listing some Jobs opportunities but because most job-titles have the same name (product designer) I would like to prevent WordPress from adding -1, -2, -3, -4 after each slug over time and that's why I was thinking about adding the %post_id% behind the %post-name% for only this new custom post type. A post_id should be an unique number so that would prevent ugly urls in the long run.

This is what I have, only the post type setup, but right now I think I need to work with rewrites and stuff.

    function jobs() {

    $labels = array(
        'name'                => _x( 'Jobs', 'Post Type General Name', 'theme-name' ),
        'singular_name'       => _x( 'Job', 'Post Type Singular Name', 'theme-name' ),
        'menu_name'           => __( 'Jobs', 'theme-name' ),
        'parent_item_colon'   => __( 'Parent Job:', 'theme-name' ),
        'all_items'           => __( 'All Jobs', 'theme-name' ),
        'view_item'           => __( 'View Job', 'theme-name' ),
        'add_new_item'        => __( 'Add New Job', 'theme-name' ),
        'add_new'             => __( 'Add New', 'theme-name' ),
        'edit_item'           => __( 'Edit Job', 'theme-name' ),
        'update_item'         => __( 'Update Job', 'theme-name' ),
        'search_items'        => __( 'Search Job', 'theme-name' ),
        'not_found'           => __( 'Not found', 'theme-name' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'theme-name' ),
    );
    $rewrite = array(
        'slug'                => 'vacatures',
        'with_front'          => false,
        'pages'               => false,
        'feeds'               => false,
    );
    $args = array(
        'label'               => __( 'vacatures', 'theme-name' ),
        'description'         => __( 'Post Type Description', 'theme-name' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'thumbnail', 'revisions', 'custom-fields', ),
        'taxonomies'          => array( 'category' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => false,
        'show_in_admin_bar'   => false,
        'menu_position'       => 9,
        'menu_icon'           => 'dashicons-welcome-learn-more',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'rewrite'             => $rewrite,
        'capability_type'     => 'post',
    );
    register_post_type( 'vacatures', $args );

}
// Hook into the 'init' action
add_action( 'init', 'jobs', 0 );

any help would be so thankful!

EDIT

I'm a bit further now and I have a feeling I'm getting close to what I need.

I've added the code below and the result is almost good. It appends the post_id on the end of the slug only problem it's twice/double.

I want this:

http://example.com/vacatures/product-designer/123/

But instead it does this:

http://example.com/vacatures/product-designer/123/123/

function fix_permalink( $post_link, $id = 0 ) {

    add_rewrite_rule(
        '^vacatures/',
        'index.php?post_type=vacatures&p=',
        'top'
    );

    $post = get_post($id);
    if ( is_wp_error($post) || $post->post_type != 'vacatures' ) {
        return $post_link;
    }

    empty ( $post->slug )
        and $post->slug = sanitize_title_with_dashes( $post->post_title );

    return home_url( user_trailingslashit( "vacatures/$post->slug/$post->ID" ) );

}
add_filter( 'post_type_link', 'fix_permalink' );

Upvotes: 1

Views: 2913

Answers (1)

Paxjah
Paxjah

Reputation: 41

Looking at your code, it looks as though you've rewritten the slug to imitate the post ID and yet in the last line of code you state the new permalink should be the post_type followed by the slug (which is now the ID) followed by the ID (the reason for the double).

I did a few tests and indeed either removing the slug or ID from the new permalink structure it did remove the duplicate. But I could not get the_permalink() to correctly link to a single view of my custom post type post (Eg. within an archive page using 'the_permalink()' did not link to my single.php for that post type), so I cannot say that I could get your code to work for me.

But I have been using this snippet for a while and it does what you'd like.

Your rewrite:

'rewrite' => array( 'slug' => 'vacatures' )

The function:

// Rewrite permalink structure
function vacatures_rewrite() {
    global $wp_rewrite;
    $queryarg = 'post_type=vacatures&p=';
    $wp_rewrite->add_rewrite_tag( '%cpt_id%', '([^/]+)', $queryarg );
    $wp_rewrite->add_permastruct( 'vacatures', '/vacatures/%cpt_id%/', false );
}
add_action( 'init', 'vacatures_rewrite' );

function vacatures_permalink( $post_link, $id = 0, $leavename ) {
    global $wp_rewrite;
    $post = &get_post( $id );
    if ( is_wp_error( $post ) )
        return $post;
        $newlink = $wp_rewrite->get_extra_permastruct( 'vacatures' );
        $newlink = str_replace( '%cpt_id%', $post->ID, $newlink );
        $newlink = home_url( user_trailingslashit( $newlink ) );
    return $newlink;
}
add_filter('post_type_link', 'vacatures_permalink', 1, 3);

New permalinks structure is: http://domain.com/vacatures/208/

I hope this helps you.

Upvotes: 4

Related Questions