This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Ignore unlinked Attachments

#21405
Ernest MarcinkoErnest Marcinko
Keymaster

Hi,

Yep, that should filter the live results. I have made a correction to the code right after I posted, you might still got the original post. Can you please verify, that this is the code:

add_filter('asp_results', 'asp_results_remove_unlinked_attachments');
function asp_results_remove_unlinked_attachments($results) {
    foreach ($results as $k => &$r) {
        if ( $r->content_type == 'attachment' ) {
            $parent_id = wp_get_post_parent_id( $r->id );
            if ( is_wp_error($parent_id) || empty($parent_id) ) {
                // Remove the post from the results list
                unset($results[$k]);
            }
        }
    }
    return $results;
}

You don’t have to clear the cache, this should work effective immediately.