What is The right way to close $query->is_home() loop

511 viewsProgramingWeb DevelopmentWordpress
0

What is The right way to close $query->is_home() loop

I’ve tried many ways to close this loop, but my page always crashes or it doesn’t work properly. I’m new to WordPress and there may be some wrong logic.

The goal is that when I publish a post with the category “Picture of the Week”, a copy of 1062 posts will be automatically published.

I tried to Google this issue, but do not understand how to close the loop? Or is it still a problem?

function poll_after_weekspicture($query) {
        if ( $query->is_home() ) {
            
            if ( get_post_type( $post->ID ) == 'post' && in_category( 
                'weekspicture') ) {
                
                    $new_post_id = copyofpoll($post)

                    // Give the category 14
                    wp_set_post_categories( $new_post_id, array('14') ); 

                    // Here I am trying to close the loop
                    wp_reset_postdata();
                } 
        }

        return $query;

  

in case it matters, here the function copyofpoll(). The $weekspicture_id does not yet do anything, that is no a mistake.

 function copyofpoll($weekspicture_id){
    $title   = get_the_title('1062');
        $oldpost = get_post('1062');
        $post    = array(
          'post_title' => 'Automatic poll',
          'post_status' => 'publish',
          'post_type' => $oldpost->post_type,
          'post_author' => 1
        );
        $new_post_id = wp_insert_post($post);
        // Copy post metadata
        $data = get_post_custom('1062');
        foreach ( $data as $key => $values) {
          foreach ($values as $value) {
            add_post_meta( $new_post_id, $key, $value );
          }
        }
    
        return $new_post_id;    
      }

 

 

Changed status to publish