PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 18, 2022

[FIXED] Change Wordpress Posts' last_modified date and time with WP Query

 March 18, 2022     php, wordpress     No comments   

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.

  1. I have 6 posts and i get only 5
  2. 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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing