Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

WordPress - Loop through selected posts

lorro Dec 28th, 2016 58 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  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();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top