Archive-Pro-Matic Pretty URL Rewrite

The goal is to change an ugly archive url into a pretty url. For example here is a quarterly custom post type archive:

    The links are in a format of:

    https://spacedonkey.de/date/2012/?q=4&post_type=monkey

    And they would look better with something like:

    https://spacedonkey.de/monkey/2012/Q4

    To accomplish this, first the Add rewrite rules must be checked on the Archive-Pro-Matic settings page:

    After the CPT Rewrite Rules has been checked and saved. It might be necessary to flush the rewrite rules by simply navigating to:
    Dashboard > Settings > Permalinks – do nothing, and re-save the permalink settings.

    Second a custom url rewrite function must be added to the child-theme’s function.php file. This function uses a new filter that has been added to manually overwrite the url that archive-pro-matic uses:

    add_filter('apm_archive_link', 'pretty_my_archive_url');
    function pretty_my_archive_url($url){
        $url_args = explode('?', $url);
        if(!empty($url_args[1])){
           parse_str($url_args[1], $args);
           if(!empty($args['year']) && !empty($args['q'])){
              $url = $url_args[0].$args['year'].'/Q'.$args['q'].'/';
           }
        }
        return $url;
    }

    Now, when we display a quarterly archive for a custom post type, the urls are prettier:

    [archives type="quarterly" post_type="donkey" /]

      And here is a test of just using the standard yearly archive for a custom post type:

      [archives post_type="donkey" /]