Sort by date Function ? post date randomizer wordpress

Question
function pdr_randomize() {
    $randomize = isset( $_REQUEST['randomize'] ) ? $_REQUEST['randomize'] : '';
    if ( isset( $_GET['pdr_rd_nonce'] ) && wp_verify_nonce( $_GET['pdr_rd_nonce'], 'pdr_rd_randomize' ) ) {
        if ( $randomize == 'yes' ) {
            $date_format       = 'Y-m-d H:i:s';
            $date1             = get_option( 'pdr_date1', date( $date_format, strtotime( "-1 year", time() ) ) );
            $date2             = get_option( 'pdr_date2', date( $date_format, time() ) );
            $cr_type           = get_option( 'pdr_post_type' );
            $set_modified_date = get_option( 'pdr_set_modified_date', 1 );
            //* Get all the posts
            $posts = get_posts( [ 'numberposts' => - 1, 'post_status' => 'any', 'post_type' => $cr_type ] );
            // Also set the modified date?
            if ( $set_modified_date ) {
                add_filter( 'wp_insert_post_data', 'pdr_set_post_modified_to_published' );
            }
            foreach ( $posts as $post ) {

                //* Generate a random post dates
                $random_date = mt_rand( strtotime( $date1 ), strtotime( $date2 ) );
                //* Format the date that WordPress likes
                $post_date = date( $date_format, $random_date );

                // We only want to update the post date
                $update = [
                    'ID'            => $post->ID,
                    'post_date'     => $post_date,
                    'post_date_gmt' => null,
                ];

                //* Update the post
                wp_update_post( $update );

            }
            // Stop setting the modfied date
            if ( $set_modified_date ) {
                remove_filter( 'wp_insert_post_data', 'pdr_set_post_modified_to_published' );
            }
            // Notice of sucess after action
            add_action( 'admin_notices', 'pdr_acf_notice' );
        }
    }
}

function pdr_set_post_modified_to_published( $data ) {
    if ( isset( $data['post_date'] ) ) {
        $data['post_modified'] = $data['post_date'];
    }

    if ( isset( $data['post_date_gmt'] ) ) {
        $data['post_modified_gmt'] = $data['post_date_gmt'];
    }

    return $data;
}

This is code from plugin post date randomizer wordpress,

i need sort randomize post date but with sort by date feature.

so when randomize date post is still in the same posting order.

then i try added

'orderby' => 'date', 'order' => 'ASC',

in $posts = get_posts(

but why still randomize posting order?
example :
my current post order with title
episode 1, episode 2, episode 3,…

when i using this plugin it will random post date and post order become
episode 2, episode 1, episode 3

0
, , , , johan 3 years 2020-07-18T04:10:38-05:00 0 Answers 67 views 0

Leave an answer

Browse
Browse