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

Reply To: Ignore unlinked Attachments

#21518
AdditiveFXAdditiveFX
Participant

To be clear, this is the working code:

add_filter('asp_results', 'asp_results_remove_unlinked_attachments');
function asp_results_remove_unlinked_attachments($results) {
    foreach ($results as $k=>&$r) {
        if ( isset($r->post_type) && $r->post_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;	
}