0

How to get a custom save_post function to return the default $post_id, $post, and $status for items with a post status of 'trash'?

Everything I return, no matter which way I try, spits out Warning: array_map(): Argument #2 should be an array in /post.php on line 1980

For items in the trash when restoring from trash or changing the post status if I play with different statuses. Seems like any return is just not returning the default array.

Need to get back to the basics here. Below is the code that works except for when restoring items from the trash, bugs out.

Thanks.

 //add_action('init', array( $this, 'init' ), 10, 2 );

add_action( 'save_post', 'update_children', 10, 2);
function update_children( $post_id, $post ) {

    if ( 'trash' != $post->post_status ){

        // Is a top level post
        if( 0 === $post->post_parent ) {
            $children = array(
                'post_parent'   => $post->ID,
                'post_type'         => 'any',   //you can use also 'any'
                'fields'            => 'ids',   // Only Return IDs
                'post_status' => 'publish',
            );
            global $post;

            if ((is_array($children))&&(!empty( $children ))){
                $the_query = new WP_Query( $children );
            }
            // The Loop
            if ( $the_query->have_posts() ) :
            while ( $the_query->have_posts() ) : $the_query->the_post();
            $pages[] = get_the_ID();
            //$pages = array();         

            if ((is_array($children))&&(!empty( $children ))) {     
                foreach( $children as $child_ids ) {
                    remove_action( 'save_post', 'update_children', 10, 2);
                    wp_update_post( $pages );
                    add_action( 'save_post', 'update_children', 10, 2);
                }           
            }
            endwhile;
            endif;      
        }
    }
}
  • 3
    You need to post the code you've tried or it will be hard for anyone to help. There's nothing inherent to save_post that would cause this issue, so it's something you're doing. If we can see code that's causing the issue someone might be able to offer a solution. Without that it would just be a shot in the dark. – Jacob Peattie Mar 15 '18 at 6:57
  • Thanks, just added. When restoring posts from the trash, for some reason it's saying it expects $post should be an array and it isn't. I tried to include $status too, but no luck. – alice Mar 15 '18 at 13:53
  • 1
    wp_update_post expects an associative array with elements that match the columns of the posts table. – Milo Mar 15 '18 at 15:41
  • Agreed, but so far everything works for every action but untrash. I'm able to get my array of child post IDs and update them all just fine and even apply another function to them. The problem only happens when I untrash, and as I understand, the action shouldn't be running for trash posts anyway. How do I either properly exclude the trash items or return the correct format for the associative array? – alice Mar 15 '18 at 19:30
0

So, I had to explicitly define the key value pairs to get it to work when untrashing. Not sure why it worked everywhere else though as just an array of ids.

added:

$thisupdate = array (
                    'fields'      => $pages,
                    'post_status' => 'publish'
);

wp_update_post( $thisupdate );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.