Issue
I need to change the last modified date and time of all of my blog's posts in WordPress. So far i have....
add_action( 'wp', 'asd' );
function asd()
{
$post_list = get_posts( array(
'post_per_page' => '-1'
) );
foreach ( $post_list as $post ) {
// $posts[] += $post->ID;
$postID = $post->ID;
$datetime = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
echo $postID . ' ||| ' . $datetime . '<br>';
global $wpdb;
$wpdb->query( "UPDATE `$wpdb->posts` SET `post_modified` = '" . $datetime . "' WHERE ID = " . $postID);
}
}
I get no errors or whatsoever. I am using "echo" for debugging purposes. I have two problems.
- I have 6 posts and i get only 5
- It seems that no update is happening in the database, for the last modified field
Any help would be appreciated.
Solution
you use this code.( paste it in your theme function.php)
add_action('init','change_mypost_date');
function change_mypost_date(){
$args = array(
'post_type' => 'property_listing',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$datetime = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
$current_post = array(
'ID' => get_the_ID(),
'post_modified' => $datetime
);
// Update the post into the database
wp_update_post( $current_post );
}
wp_reset_postdata();
}
}
Answered By - Naveen Giri
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.