Advertisement
ozh

Update posts regex

ozh
Aug 14th, 2011
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Template name: Update posts regex
  4.  */
  5.  
  6. // query for posts
  7. query_posts( array(
  8.     'post_type' = 'any', // get all post types
  9.     'posts_per_page' = -1, // all posts
  10.     'nopaging' = true // no pagination
  11.     )
  12. );
  13.  
  14. // loop
  15. if ( have_posts() ) : while ( have_posts() ) : the_post();
  16.  
  17.     // set vars
  18.     $pattern = '/\[tab: .+? [0-9]\]/'; // regex pattern to match
  19.     $replacement = '<!-- nextpage -->'; // replacement string
  20.     $string = get_the_content(); // post content
  21.  
  22.     // match and replace
  23.     $updated_content = preg_replace($pattern, $replacement, $string);
  24.  
  25.     // Set new post content
  26.     $my_post = array();
  27.     $my_post['ID'] = get_the_ID();
  28.     $my_post['post_content'] = $updated_content;
  29.  
  30.     // Update the post into the database
  31.     $result = wp_update_post( $my_post );
  32.  
  33. endwhile; endif;
  34.  
  35. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement