Advertisement
lorro

WordPress - Loop through selected posts

Dec 28th, 2016
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.78 KB | None | 0 0
  1. <?php
  2.   // select some posts and loop through them to do something
  3.   // code goes in functions.php for your child theme
  4.  
  5.   // see https://codex.wordpress.org/Template_Tags/get_posts for all possible arguments
  6.   $args = array(
  7.    'post_type' => 'post',
  8.    'post_status' => 'any',
  9.    'numberposts' => -1 );
  10.  
  11.   $selected_posts = get_posts( $args );
  12.  
  13.   foreach( $selected_posts as $post ) {
  14.     setup_postdata($post);
  15.     $post_id = get_the_ID();
  16.     set_post_type( $post_id, 'product' );
  17.     add_post_meta( $post_id, '_price', ' >>> PRICE <<< ', true);
  18.     add_post_meta( $post_id, '_regular_price', ' >>> PRICE <<< ', true);
  19.     add_post_meta( $post_id, '_visibility', 'visible', true);
  20.     add_post_meta( $post_id, '_stock_status', 'instock', true);
  21.   }
  22.  
  23.   wp_reset_postdata();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement