• nr2012

    (@nr2012)


    Hi there

    I have a question about getting the permalink structure of custom post types.
    Because I use WordPress with its REST API and a Vue.js Frontend, I had to rewrite the preview link a bit to point to my frontend.

    I have a custom post type registered where I rewrite its slug:

    'rewrite' => array('slug' => 'company/media/%year%','with_front' => false),
    

    So my preview link looks like:
    https://domain.com/company/media/2019/my-cpt-post-name/?preview_id=1272&preview_nonce=90af8799eb&preview=true
    I prepare this by calling get_permalink($id); which gives me that part https://domain.com/company/media/2019/my-cpt-post-name.

    Which is all good.
    But now if I add a new news post (My custom post type) I need the get it’s permalink strucutre to prepare the preview link like the one above.
    Only in this case when calling
    get_permalink($id); this will return something like this:

    https://domain.com/?post_type=news&p=1283

    As long as it is not published the get_permalink($id) will remain like this.
    How could I possibly get the permalink structure of this custom post type?

    I know that I can call this:
    $permalink_structure = get_option('permalink_structure');

    But this will only give me the permalink strucutre of the default post and not of my custom post type.
    So how could I get company/media/%year%?

    Thank you in advance.
    Cheers

    • This topic was modified 6 years ago by nr2012. Reason: chore
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You can get the rewrite slug for any post type from the global $wp_post_types array. Lets say your post type is “news”. You could do

    global $wp_post_types;
    $my_slug = $wp_post_types['news']->rewrite['slug'];
    Thread Starter nr2012

    (@nr2012)

    Thank you. I will try out soon and get back here.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Retrieve permalink structure of custom post type’ is closed to new replies.