Give attachments an archive page


I’m trying to override WP’s default URL structure for attachments where, if they’re attached to a post, the URL is /post-slug/attachment-slug/ and if not it’s simply /attachment-slug/.

What I’d like instead is for attachments to behave like posts in that they have an archive and all URLs point to /archive-slug/attachment-slug/.

I’ve found a new (with 4.4 I believe) filter that allows you to modify post type options but it doesn’t seem to work as advertised;

add_filter('register_post_type_args', function ($args, $postType) {
    if ($postType == 'attachment'){
        $args['has_archive'] = true;
        $args['rewrite'] = [
            'slug' => 'media'

    return $args;
}, 10, 2);

If I var_dump($args) they do in fact look correct (has_archive is true etc) but it doesn’t appear to have any effect on the URLs at all.

This might not be very surprising if this comment by the devs is correct; “Does not apply to built-in post types”

So my question is how can I still accomplish this?

I’ve also tried just modifying the post type object in the init hook, but it won’t bite:

$obj = get_post_type_object('attachment');

$obj->has_archive = true;
$obj->rewrite = [
    'slug' => 'media'
, powerbuoy 3 years 2020-08-21T03:10:32-05:00 0 Answers 47 views 0

Leave an answer