Skip to content

Delete Descendant Galleries Automatically

If you have created a hierarchy of sub galleries in Sunshine, you may want to delete all child galleries if you delete the top most level gallery. This does not happen by default in WordPress (thus Sunshine).

Moving a gallery to the Trash leaves its sub galleries where they are, still attached to the trashed gallery. But permanently deleting that gallery, either with "Delete Permanently" or by emptying the Trash, moves every sub gallery up a level rather than deleting it. On a deep hierarchy that can drop a lot of galleries at the top of your list at once.

The snippets below change that. There are two, because trashing a gallery and permanently deleting one are separate actions in WordPress. Add both if you want every route covered.

Trashing a gallery

This trashes all child and descendant galleries whenever you move a gallery to the Trash.

add_action( 'wp_trash_post', 'sunshine_trash_descendant_galleries' );
function sunshine_trash_descendant_galleries( $post_id ) {
    if ( get_post_type( $post_id ) !== 'sunshine-gallery' ) {
        return;
    }

    static $is_trashing = false;

    if ( $is_trashing ) {
        return;
    }

    $is_trashing = true;

    $descendant_ids = sunshine_get_gallery_descendant_ids( $post_id, array( 'publish', 'private', 'draft', 'pending', 'future', 'trash' ) );

    if ( ! empty( $descendant_ids ) && is_array( $descendant_ids ) ) {
        foreach ( $descendant_ids as $descendant_id ) {
            wp_trash_post( $descendant_id );
        }
    }

    $is_trashing = false;
}

Learn how to add this custom code to your WordPress website

Permanently deleting a gallery

This one covers "Delete Permanently" and emptying the Trash, which is the route that otherwise promotes your sub galleries to the top level.

add_action( 'before_delete_post', 'sunshine_delete_descendant_galleries' );
function sunshine_delete_descendant_galleries( $post_id ) {
    if ( get_post_type( $post_id ) !== 'sunshine-gallery' ) {
        return;
    }

    static $is_deleting = false;

    if ( $is_deleting ) {
        return;
    }

    $is_deleting = true;

    $descendant_ids = sunshine_get_gallery_descendant_ids( $post_id, array( 'publish', 'private', 'draft', 'pending', 'future', 'trash' ) );

    if ( ! empty( $descendant_ids ) && is_array( $descendant_ids ) ) {
        foreach ( $descendant_ids as $descendant_id ) {
            wp_delete_post( $descendant_id, true );
        }
    }

    $is_deleting = false;
}

Learn how to add this custom code to your WordPress website

Both snippets look at every level below the gallery you are removing, not just the galleries directly inside it. The list of statuses passed in the second argument is what makes them pick up sub galleries that are private, still a draft, or already in the Trash, instead of only the published ones. There is no undo on the second one, so test it on a gallery you do not mind losing before you rely on it.

Still need help?

If you have not yet found your answer in the documentation articles, please contact support

Sunshine Photo Cart for WordPress