[wp-trac] [WordPress Trac] #53336: Deleting CPT post by code (ie in a plugin) redirects to the main post type.

WordPress Trac noreply at wordpress.org
Sat Jun 5 01:30:42 UTC 2021


#53336: Deleting CPT post by code (ie in a plugin) redirects to the main post type.
----------------------------+-----------------------------
 Reporter:  princeofabyss   |      Owner:  (none)
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  General         |    Version:  5.7.2
 Severity:  normal          |   Keywords:
  Focuses:  administration  |
----------------------------+-----------------------------
 1. Create a CPT

 2. In functions.php or in a plugin, hook on 'save_post' to perform some
 calculations upon hitting the ''Publish'' button in WP-Admin...

 3. 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);
         }
     }
 }
 }}}

 4. Once the post is deleted you are redirected to the stock post type
 (?post_type=<CPT_slug> part of the URL is gone)

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53336>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list