Find us on Facebook

LightBlog
Responsive Ads Here

Thứ Sáu, 29 tháng 12, 2017

Custom post type permalink missing /post_type/ front

Question:

I have a custom post type:
register_post_type( 'tour',
    [
        'labels' => [
            'name'          => __( 'Tour shows' ),
            'singular_name' => __( 'Tour show' )
        ],
        'public'              => true,
        'menu_icon'           => 'dashicons-tickets-alt',
        'has_archive'         => 'tour', // also tried true
        'with_front'          => true,
        'exclude_from_search' => true, 
        'taxonomies'          => ['post_tag'],
        'rewrite'             => ['slug' => 'shows-on-tour'],
        'supports'            => ['title', 'editor', 'thumbnail']
    ]
);
(this has been butchered a bit while trying to make it work)
I'm having an issue where by when I do get_permalink($tour->ID) it returns:
example.com/post-title
This then redirects you to
example.com/shows-on-tour/post-title
The problem is I have a separate post type for "shows not on tour", so whenever I link to:
example.com/post-title
I end up at:
example.com/shows-not-on-tour/post-title
How can I use get_permalink on either post type and have it return the full url to the post with the correct post type on the front?

Update

In the editor, when I edit the tour it clearly says:
example.com/shows-on-tour/post-title
and the not on tour:
example.com/shows-not-on-tour/post-title
So the issue must lie with how / where I'm calling get_permalink

Answer:

In my case I was trying to get the permalink before I had set up the post types.
I double checked the Action Reference: https://codex.wordpress.org/Plugin_API/Action_Reference
And updated the hooks I was using to trigger my theme setup. Once done, the get_permalink worked as expected.

Không có nhận xét nào:

Đăng nhận xét