• Resolved lex713

    (@lex713)


    add_action('wp_trash_post', 'force_trash');
    function force_trash($postid)
    {
    	wp_delete_post($postid);
    }

    I am using this code to delete a post when the trash button is pressed. I have to run this because I have to force everything to be published and it fires every time a post is update with ‘wp_insert_post_data’. My problem is every time I hit the trash button it goes to a page that says “Error in moving the item to Trash.” But it still deletes the post.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    According to the docs, wp_trash_post fires before a post is sent to the Trash. So, you’re deleting a post before it can be trashed, causing the trashing to fail.

    According to https://developer.www.ads-software.com/reference/functions/wp_delete_post/, trash can be disabled. Adding define( 'EMPTY_TRASH_DAYS', 0 ); to wp-config.php should do that, so when you trash a post, it should just delete it (and your code will be unnecessary).

    Moderator bcworkz

    (@bcworkz)

    FWIW, there is a “trashed_post” action which fires after moving a post to trash. Could be useful if we wanted only certain posts to not be trashed and immediately deleted. But if you don’t want anything trashed, Steven’s suggestion is the way to go.

    Thread Starter lex713

    (@lex713)

    @bcworkz @sterndata So my problem stems from custom code. I fix one thing get 5 more things I need to fix. The usual coding life. So it all starts with me wanting to hide the post editor with remove_post_type_support. When I do this I can no longer publish anything. Everything is saved as a draft. So I have this code running to force everything to be published.

    function force_type_private($post)
    {
        $post['post_status'] = 'publish';
        return $post;
    }
    add_filter('wp_insert_post_data', 'force_type_private');
    

    The problem is when I try to trash something it calls the code above. My work around for that was this

    
    add_action('wp_trash_post', 'force_trash');
    function force_trash($postid)
    {
    	wp_delete_post($postid);
    }
    

    But when the line above trashes it redirects me to that page I stated earlier. So stevens answer won’t help unless there is a hook that plays after it goes to the trash but I don’t think it ever gets that far because of the code that calls when wp_insert_post_data. I don’t really care if things go to the trash or not. I just want it all to look smooth no error screens. Is there some hooks that I didn’t see earlier that I should be using instead? I feel like the code is all right I’m just calling them at wrong times.

    Also thank you for all your help I know @bcworkz has helped me with almost every issue I have had.

    Moderator bcworkz

    (@bcworkz)

    Hi lex713, I didn’t recognize you at first. You hiding the editor brought everything into focus. Your ‘wp_insert_post_data’ callback should be verifying some data before forcing the status to “publish”. For example, there might be some post types you don’t want forced to publish. At least do not force “publish” if the status was meant to be “trash”.

    Generally speaking, if you want to do something when a post’s status changes, the “transition_post_status” action is useful. By checking the old and new status values, you can do one thing or another based on the situation. For example, going from draft to publish only happens when a post is first published. Updates go from publish to publish (the transition action fires even when there is no change in status). This could be useful for example, if you wanted to send an email when first published but not when updated. Or vice versa. I’m not sure you need this action in this case, it’s just FYI.

    Thread Starter lex713

    (@lex713)

    @bcworkz so could this work I’m having a hard time finding info on hooks and what calls what so there might be some issues but I’m thinking

    
    
    function force_type_private($post)
    {
     If (did_action(‘trash_post’)  //if trying to trash post
    {
    }
    Else 
    {
     if ($post[‘post_title’] = ‘ ‘ || ‘autodraft’ //check if title is empty or autodraft
     {
     }
      Else 
          {
      $post['post_status'] = 'publish';
        return $post; //if both both checks are false set stays to published
           }
    }
       
    }
    add_filter('wp_insert_post_data', 'force_type_private');
    
    
    • This reply was modified 4 years, 2 months ago by lex713.
    Moderator bcworkz

    (@bcworkz)

    The “trash_post” action hasn’t yet fired when your callback executes, AFAIK. Fortunately, there’s an easier way to check if trashing is intended, check if $post['post_status'] != 'trash'. If it is not trash and the title is either empty or autodraft, then you may force publish. Be sure to check with equivalency operator == and not assignment =. Be aware of operator precedence — is || evaluated before or after ==? You might want to set up some test statements to be sure the logic is doing what you intend.

    I use a test page based on a custom template in my child theme for such evaluations. There’s a section on the template where I can insert experimental code to help me understand certain constructs or functions.

    You’re only returning $post under certain conditions. In filter callbacks you must always return the 1st passed value whether it has been changed or not.

    Thread Starter lex713

    (@lex713)

    @bcworkz I came up with this but it isn’t working. I believe it doesn’t work because it is checking if it is in the trash but the problem is I need to check if it’s trying to go to the trash.

    
    function force_type_private($post) 
    {
     $post['post_status'] != 'trash'
      {
    	$post['post_status'] = 'publish';
    	return $post;	
      {
    }
    add_action('wp_insert_post_data', 'force_type_private', 20);
    
    Moderator bcworkz

    (@bcworkz)

    That should work, the action fires when updating post status to “trash”. If the post were already in trash, this action would not fire. However, your code is missing the needed if() statement, plus the curly braces are unbalanced — 3 opening, only one closing.

    The return statement should not be inside a conditional block. You must return $post whether it had been altered or not.

    In addition to checking that status is not “trash” don’t you also want to check if the title is empty or “autodraft” before force publishing?

    Thread Starter lex713

    (@lex713)

    Got it working with this

    function force_type_private($post) 
    {
    	if ($post ['post_status'] != 'trash')
    	{
    		 if (!empty(get_field('recipe_name', $post->ID))) 
    		{
    	       $post['post_status'] = 'publish';
    		}
    	}
    return $post;
    }
    add_action('wp_insert_post_data', 'force_type_private', 20);
    • This reply was modified 4 years, 2 months ago by lex713.
    • This reply was modified 4 years, 2 months ago by bcworkz. Reason: code fixed
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Post delete but goes to page saying it can’t delete’ is closed to new replies.