Custom Post Type Settings page, choose page to display archive

There is a dirty (actually dirty as hell) way to attach a ordinary WordPress page as archive page from a custom post type settings page.

You need to unregister post type first, and create again. If you don’t use custom arguments or if you brave enough try this code.

Add this code fragment end of your settings page.

if(isset($_GET['settings-updated']) && $_GET['settings-updated'] == true){
    //Dirty as hell
    $esettings = get_option ( "events_settings_options" );
    $getslug = get_post_field('post_name',$esettings['events_page']);

    unregister_post_type('events');
    register_post_type('events',array(
        'public' => true,
        'rewrite' => array('slug'=>'events'),
        'has_archive' => $getslug,
    ));
    //Needed for automated rewrite rules
    flush_rewrite_rules();
    //To test the functionality
    echo get_post_type_archive_link( 'events' );
}

Leave a Comment