Make WordPress Core

Opened 9 months ago

Last modified 9 months ago

#52759 new feature request

Add a filter to get_available_post_mime_types() function to allow overriding extremely slow query.

Reported by: maciejmackowiak Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Media Keywords: good-first-bug has-patch
Focuses: performance Cc:


On sites sites with lot of posts the query used in get_available_post_mime_types is extremely slow.
It would be nice to have a filter that will allow us to override this query and for example cache the results.

It could be something simillar to this filter:

function get_available_post_mime_types($type = 'attachment') {
        global $wpdb;
        $types = apply_filters('get_available_post_mime_types', null, $type);
        if ( ! is_array( $types ) ) {
                $types = $wpdb->get_col($wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type ) );
        return $types;

Change History (9)

#1 @johnbillion
9 months ago

  • Component changed from General to Media
  • Focuses performance added
  • Keywords needs-patch good-first-bug added

#2 @archon810
9 months ago

+1 for this.

For us, it adds an additional 0.35s or so (right now, more if db is loaded).

FROM wp_posts
WHERE post_type = 'attachment'
> OK
> Time: 0.35s

On the grid view it is called from here:

It calls this function:

And here is the query:

We were able to cache and greatly speed up this query already:

SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
FROM wp_posts
WHERE post_type = 'attachment'
AND post_status != 'auto-draft'
AND post_status != 'trash'
ORDER BY post_date DESC

and I hope with the new proposed filter, we will be able to speed up the site even more.

This ticket was mentioned in PR #1088 on WordPress/wordpress-develop by maciejmackowiak.

9 months ago

  • Keywords has-patch added; needs-patch removed

This ticket was mentioned in Slack in #core-media by antpb. View the logs.

9 months ago

#6 @Mista-Flo
9 months ago

Hi there,

I'm definitely for a filter to short circuit this slow query, but I think first we need to fix this slow query by implementing a cache, wether it use the internal cache API system from Core or the Transient API, (check the slack thread we started for further comments)

Version 0, edited 9 months ago by Mista-Flo (next)

#7 @archon810
9 months ago

Great idea 💡.

#8 @maciejmackowiak
9 months ago

check the slack thread we started for further comments

Can you point me to that thread, please?

Note: See TracTickets for help on using tickets.