1. Pancho Perez

    Now is the thirdth project I need ot create a Template to list all files uploaded to the Media Library, to found a solution I have to serach and try to much.

    Why we donĀ“t have an option to use archive-attachment.php as we have attachment.php for single attachments?

    Then it could be easy to filter the files using post_mime_type.

    Other could be filtered the attachments if they are inside a post or not.

    I know also need to create a new url like wordpress/attachment or /attachments.

    Then make a gallery image or a download files archive page could be very very easy :)

    Posted: 6 years ago #
  2. Mike

    I could use this kind of functionality immensely!

    Posted: 4 years ago #
  3. khodadadi

    Hi Pancho Perez
    I know that the topic is for 3 years ago and I don't know whether you find a solution to this problem or not.
    But as I was searching for solution and I could not find anything on the web, I solved it on my own and I want to share it here.

    The solution is very simple. WordPress registers the default post types (post,page,attachment and etc) with arguments that don't allow us to have archive pages for these post types.

    you should add archive-{post_type}.php template files to your template folder (archive-post.php or archive-attachment.php) and then add this code to your functions.php file:

    function dt_change_default_post_type_args($args, $post_type){
    	if($post_type == 'attachment'){
    		$args['has_archive'] = true;
    		$args['rewrite'] = array('slug'=>'media');
    	if($post_type == 'post'){
    		$args['has_archive'] = true;
    		$args['rewrite'] = array('slug'=>'blog');
    	return $args;
    add_filter( 'register_post_type_args', 'dt_change_default_post_type_args', 10, 2 );

    you can change rewrite argument to have your own slug or just set it boolean true to use post type default slug.
    at last remember to save your permalink settings again after saving the functions.php file.

    Posted: 3 years ago #
  4. powerbuoy

    @khodadadi this looked really promising, but I can't get it to work at all.

    I've added the code and confirmed that it runs (I var_dumped the whole $args array and it contains has_archive and slug) but the URLs refuse to change.

    What's more, images uploaded to a specific post will have that post's URL as a prefix. I'd like a solution where every image uploaded ends up at /images/the-image/ regardless.

    Posted: 3 years ago #
  5. Awesome! That really is simple but I just wasn't able to figure that out.

    I need it as an upload for photographs and this really helps. Nevertheless I have the same issue than powerbuoy and don't want all the photos / attachments which are uploaded in a specific post to have the URL as a prefix. Any ideas ?

    Posted: 3 years ago #
  6. khodadadi

    Dear @powerbuoy and @parkerarrowfotografie
    Let me ask you this question: Do you use the_permalink() function in the main loop for print the files' urls? If your answer is yes, then use wp_get_attachment_url() function for this purpose.
    And finally if you want to display special file mime type in your media archive page you can use codes like below.

    function dt_change_pre_get_posts_arguments($query){
    	if( $query->is_main_query() ){
    		if( $query->is_post_type_archive('attachment') ){
    			$query->set('post_mime_type', 'image/jpeg');
    			$query->set('post_status', 'any');
    add_action( 'pre_get_posts', 'dt_change_pre_get_posts_arguments');
    Posted: 3 years ago #
  7. entruempler

    @khodadadi: Thanks a lot, your code is working really nice to display the file mime type, great idea, very well done!

    Posted: 2 years ago #
  8. apedog


    You need to flush rewrite rules (go to Settings > Permalinks) for the new URL's to work.

    Yeah, I know it's a year late but maybe for the next person trying to implement this.

    Posted: 2 years ago #

RSS feed for this topic


You must log in to post.

  • Rating

    5 Votes
  • Status

    This idea is under consideration