Make WordPress Core

Opened 2 years ago

Closed 2 years ago

#53336 closed defect (bug) (wontfix)

Deleting CPT post by code (ie in a plugin) redirects to the main post type.

Reported by: princeofabyss's profile princeofabyss Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.7.2
Component: General Keywords:
Focuses: administration Cc:

Description

  1. Create a CPT
  1. In functions.php or in a plugin, hook on 'save_post' to perform some calculations upon hitting the Publish button in WP-Admin...
  1. Inside the hook's code include some conditional logic that under certain circumstances it will delete the CPT post that is just being created. Ie study the following piece of code:
add_action('save_post', 'import_external_data', 10, 2);
function import_external_data($post_id, $post)
{
    if ($post->post_status == 'publish' && $post->post_type == 'movie') {
        // Check if movie exists
        $movies = get_posts(array(
            'numberposts' => 1,
            'post_status' => 'publish',
            'post_type' => 'movie',
            'meta_key' => 'movie_id',
            'meta_value' => $post->post_content,
        ))[0];

        if (empty($movies)) {
            // Various actions take place here
        } else {
            wp_delete_post($post_id, true);
        }
    }
}
  1. Once the post is deleted you are redirected to the stock post type (?post_type=<CPT_slug> part of the URL is gone)

Change History (2)

This ticket was mentioned in Slack in #core by peterwilsoncc. View the logs.


2 years ago

#2 @mikeschroder
2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Hi @princeofabyss !

Thanks for submitting the ticket.

This was talked about in a triage session today.

I'll give a synopsis based on the feedback there.

The issue is that once a post is deleted, WordPress can't reliably determine what the CPT of the post was.

For this reason, the consensus was to close the ticket as wontfix.

As far as a way forward: One suggestion was to reconsider the architecture of what you're building to see if there's a way to do it without deleting the post in that location, like returning an error before the post is saved.

Thanks again, and sorry for the disappointing news -- I hope this recommendation is helpful.

Note: See TracTickets for help on using tickets.