Support

Account

Home Forums Backend Issues (wp-admin) How to delete all the post_object fields when a post item is deleted

Helping

How to delete all the post_object fields when a post item is deleted

  • How to delete all the post_object fields when a post item is deleted.

    I.e. if you have a repeater, where one of the columns is a post_object referencing a post from your blog. If later on I delete the post from the blog, how can I delete all the references on ACF to it.

    I was thinking on something like this:

    add_action('delete_post', 'my-function-on-delete');
    function my-function-on-delete($post_id) {
        $post = get_post($post_id);
        if ($post->post_type == 'my-custom-post-type') {
            //My function
           }
    }

    The thing left is how do I delete all the ACF entries that contain the $post_id ?

    Thanks for your time.

  • Hi @aldoreyes

    This is a bit of a tricky one, but is possible with some logic as such:

    1. Search the wp_postmeta table for any rows which have a value of $post_id

    2. Delete these rows!

    The database query will have to be a custom FIND sql statement, but this is easy to create thanks to the wpdb functions. You can read more about this here:

    http://codex.wordpress.org/Class_Reference/wpdb

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘How to delete all the post_object fields when a post item is deleted’ is closed to new replies.