I have a custom post type that is configured to use a permalink structure prepended with the post type name. When using
get_permalink(), the URL that is returned does not include the post type name. This is virtually identical to the scenario described in this question.
register_post_type('activity', [ ... 'public' => true, 'rewrite' => [ 'slug' => 'activities', 'with_front' => true, ], ]);
The permalink should take the form
Usually, WordPress does a pretty good job of guessing the correct post that should be displayed, and redirects the former to the latter. However, if the post is set to be private, that first URL results in a 404, even though the second URL is visible to the logged in user.
- Is it normal for
get_permalink()to omit the post type slug from the URL it returns, resulting in a redirect? If not, what's causing it?
- If it is normal, how can I fix the 404 so it also redirects for logged in users?
The code I'm working with is all defined in a custom plugin. The
register_post_type() snippet above is called by an
init hook action, while the problem
get_permalink() calls are in the
plugins_loaded hook action.
The author of the question linked above states that his issues was caused by querying the permalink before the post type had been registered. Are the hooks I'm using in my code sufficient to avoid this cause?