Making a Custom Post Type Publish Loop

I want to re-publish all previously published products of a custom post type. Let's say the post type is "events"

This is what I was thinking something like this:

$args = array( 'post_type' => 'events', 'post_status' => 'publish', // get only published posts? 'posts_per_page' => -1 );$posts = new WP_Query( $args ); if ($posts -> have_posts() ) {
while ( $posts -> have_posts() ) { wp_publish_post(NEED TO GET ID) } } wp_reset_query();  The thing I can't figure out how to get the id in the loop or how to get just published posts (took an educated guesss) *Edited * $args = array(
'post_type' => 'events',
'post_status' => 'publish', // get only published posts?
'posts_per_page' => -1
);
$posts = new WP_Query($args );
if ( $posts->have_posts() ) { while ($posts->have_posts() ) {
$posts->the_post(); wp_publish_post($post->ID)
}
}

-

if ( $posts->have_posts() ) { while ($posts->have_posts() ) {
$posts->the_post(); wp_publish_post($post->ID)
}
}


That should do it. You need the call to the_post to walk through the post set properly. It also sets up the $post object. That should set all of the posts to 'publish', which is what wp_publish_post does. I guess I am not sure what you mean by "re-publish". Possibly you mean "alter the publication date". To do that you need to use wp_update_post and change the post_date and post_date_gmt. if ($posts->have_posts() ) {
while ( $posts->have_posts() ) {$posts->the_post();
wp_update_post(array('ID'=>$post->ID,'post_date'=>'','post_date_gmt'=>'')) } }  Setting the dates to nothing will cause it to default to "now"-- just tested that. If you are trying to get these to show up again in a news feed, that still won't do it. You have to edit the guid for that but doing so is highly discouraged. Also, I'd recommend you name your variable something other than $posts. WordPress uses that name. You are clobbering the default variable.

-
so the code now looks like revised post above? –  Chris Nov 24 '12 at 1:26
I am not sure what your comment means. –  s_ha_dum Nov 24 '12 at 5:44
I revised the code in my original post (see the bottom of it). I am wondering if thats now the correct code? –  Chris Nov 24 '12 at 12:43
Well, does it work? Does it do what you want? –  s_ha_dum Nov 24 '12 at 15:39