0

Hi I am building a plugin and am not as familiar with wordpress hooks as I would like. The code would not be used to display on a page rather just setting in the database. I am looking for a hook or snippet that will help me do the following:

  1. grab all posts ids with post_type = 'post' before a certain date from current date into an array
  2. foreach all those in array and set each in array post_type = 'customposttype'
  3. do any processing that wordpress requires to clean up after this "hook". (I am not sure if this needs to be done.)

Since I am building a plugin, another plugin will not help me but I am definitely open to suggestions. Thank you!

3
  • When will your code run? How often? Feb 19, 2016 at 1:55
  • it will run once a week. Feb 19, 2016 at 2:30
  • WP_Query + date_query + 'fields' => 'ids' + wp_update_post() === 'solution to problem' ;-) Feb 19, 2016 at 5:50

1 Answer 1

0

Thank to @pietergoosen for "jokingly" giving me the answer.

 function gm_gather_posts_in_array() {
    $set_days_before = '1275';
    $posts_to_be_processed = date('Y-m-d', strtotime("today - $set_days_before days"));
    $args = array( 
            'post_type'      => 'post',
            'post_status'    => 'publish',
            'orderby'        => 'date',
            'order'          => 'DESC',
            'posts_per_page' => -1,
            'fields'         => 'ids',
            'date_query'     => array(
                            array(
                                'before' => $posts_to_be_processed
                            )
                    )
            );
    $posts_to_be_processed = new WP_Query( $args );

    return $posts_to_be_processed;

    }


function gm_update_posts_to_be_archived() {
        $posts_ready_to_be_processed = gm_gather_posts_in_array();
             if ( $posts_ready_to_be_processed->have_posts() ) {
                while ( $posts_ready_to_be_processed->have_posts() ) {
                    $posts_ready_to_be_processed->the_post();
                        $process_post = array(
                        'ID'           => get_the_id(),
                        'post_type'   => 'custom_post_type',
                        );

                        wp_update_post( $process_post ); 

                    }

                }


            wp_reset_postdata();
}

   echo gm_update_posts_to_be_processed();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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