0

I need to update all posts on my wordpress site on a scheduled time (every day). So I wanted to create a new page template which I can call everyday through a cronjob to update all the posts at once.

I have already created the page and the cronjob, but the code I add inside doesn't seem to be working. Can you please let me know what is wrong with my code?

    <?php
/**
 * Template Name: POSTUP
 *
 */

$my_posts = get_posts( array('post_type' => 'post', 'numberposts' => -1,))
$post_id = get_the_ID();

foreach ( $my_posts as $my_post ) {
    $updated_post_info = array(
      'ID'           => $post_id,
      'pinged'       => '',
    );

    wp_update_post( $updated_post_info );
}
?>
6
  • Why do you want to update all post everyday without no modifications? What are you trying to achieve here? Can you be more specific?
    – Konsole
    Aug 9 '14 at 12:37
  • OK, then here is the problem: i have a custom article generator (plugin)... its working fine but the content does not appear on the post editor or the front end except when i click on the UPDATE button. SO the plugin is generating new articles every day, but the content doesn't appear if i don't click on the update button for each post... i have more than 1200 posts that needs updating and more coming posts that should be updated automatically... that's why i am trying to automate the process of updating all posts everyday Aug 9 '14 at 13:35
  • The plugin would have added some hook while updating the post. Using wp_update_post might not work.
    – Konsole
    Aug 9 '14 at 13:42
  • is there a way to update all posts by changing something that will not effect the post content, title and permalink? Aug 9 '14 at 13:44
  • Is there anyone else who can provide a good answer? please do not recommend me to change plugin or modify it... i want to update all posts at once and i know its possible...please help. Aug 10 '14 at 5:31
1

This is what I used, if I'm understanding you correctly.

UPDATE wp_posts SET post_date=NOW(),post_date_gmt=NOW(),post_modified=NOW(),post_modified_gmt=NOW();

Worked for me.

0

You aren't actually doing anything to the post. You need to create an array with updated info and pass that to wp_update_post. Bulding on the example from the wp_update_post codex page:

$my_posts = get_posts( array('post_type' => 'post', 'numberposts' => -1,))

foreach ( $my_posts as $my_post ) {
    $updated_post_info = array(
      'ID'           => $my_post->ID,
      'post_content' => 'This is the updated content.'
    );

    wp_update_post( $updated_post_info );
}
3
  • The code you are talking about above doesn't update all posts at once. Also i don't need to change any content, i just want to update all posts without making any modifications to their content. Aug 9 '14 at 11:05
  • The above code will loop through each post and update an attribute that you specify. I guess I don't fully understand what you're trying to do. Can you please clarify what you specifically mean by "update all posts"?
    – Dan Bough
    Aug 9 '14 at 11:14
  • more explanations have been provided above. Aug 9 '14 at 13:41
0

I agree with most of the other comments here that this solution doesn't seem to be doing anything that will solve your problem, but then again you know more about this issue than we do as we only understand it insofar as you've explained it here.

That being said, the issue with the code you've supplied above is a fairly simple one to fix and have it work the way you'd seem to like it to. Your code is setting the $post_id variable on the current global $post variable (which, if this is in your functions.php file, will probably come back as empty). Instead, get the ID of the posts that you're looping over. I'd also suggest using WP_Query() instead of get_posts() as it gives you a new request instead of just modifying the current one (which can also sometimes cause issued), and try again like this:

$postArgs= array(
    'post_type' => 'post',
    'fields' => 'ids',
    'posts_per_page' => -1
);
$postReq= new WP_Query(postArgs);
if($postReq->found_posts) {
    foreach($postReq->posts as $my_post) {
        //if(get_post_meta($my_post->ID, 'forced-update', TRUE)=='') {
            $updated_post_info = array(
                'ID' => $my_post->ID,
                'pinged' => ''
            );
            wp_update_post($updated_post_info);
            //update_post_meta($my_post->ID, 'forced-update', 'complete');
        //}
    }
}

I've added one other piece of code in my proposed solution: a custom meta field. Once you're able to figure out what you need to save with regard to the new article posts to get this jimmy-rig for the article-generator plugin to work, uncomment the three lines of code that are currently commented in my solution. This will make it so that the only posts that you're "fix-saving" in the future are posts that haven't already had it done to them (unnecessarily). This will save you some compute time in the long run.

Best of luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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