Ignore a filter on the media library

As per my question yesterday, I need to exclude all images from most of my WordPress queries – yesterday, I thought I needed to exclude them from all queries, until I came upon the following issue.

Excluding images using the following filter also excludes them from the media library, including the insert media dialog.

function remove_images($where) {
    global $wpdb;
    $where.=' AND '.$wpdb->posts.'.post_mime_type NOT LIKE \'image/%\'';
    return $where;
}

I still want to be able to see images in my media library and insert media dialog.

Normally, I could just put in an if (!is_admin()) clause, because media library is usually seen in the admin section.

However, I also have a wp_editor in the front end to allow my users to quickly add posts – and you can also access the media library from there.

Is there any hook or condition that I can use so that the filter isn’t included when querying the media library?

1 Answer
1

You can use is_admin() in conjunction with the admin global variable $pagenow to make sure you’re not on either the upload or media page:

function remove_images( $where ) {
    global $wpdb,
        $pagenow;

    if( is_admin() && in_array( $pagenow, array( 'upload.php', 'media-upload.php' ) ) {
        return $where
    }

    $where  .=  " AND {$wpdb->posts}.post_mime_type NOT LIKE 'image/%'";
    return $where;
}

Leave a Comment