Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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)
}
} 
share|improve this question

1 Answer 1

up vote 0 down vote accepted
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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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