Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to set a post to Draft automatically based on a custom field date.

Note: I am using the plugin Magic Fields.

Based on the following code I want to use a custom field 'expiration' to give the date.

function sfn_show_expire() { 
    global $wpdb; 
    $server_time = date('mdy'); 
    $result = $wpdb->get_results("SELECT * FROM wp_posts WHERE post_type = 'show' AND post_status = 'publish'"); 
    if( !empty($result)) {
        foreach ($result as $a) { 
            $show_time = get_the_time('mdy', $a->ID ); 
            if ( $server_time > $show_time) { 
                $my_post = array(); 
                $my_post['ID'] = $a->ID; 
                    $my_post['post_status'] = 'draft'; 
                wp_update_post( $my_post ); 
        } // end foreach 
add_action( 'init', 'sfn_show_expire' ); 

Ultimately I would like to make this in a cron job, but for now any suggestions to the code would be great.

share|improve this question
I tried changing $show_time = get_the_time('mdy', $a->ID ); to $show_time = get_post_meta('expiration', 'mdy', $a->ID ); but that did not work. – Phillip Cox Apr 19 '11 at 5:19

2 Answers 2

For cron jobs, take a look at the Transients API.

The rest could maybe be acchieved with something like the following. I have never ever used the transient API before and code's not tested.

function set_post_to_draft()
    global $post;

    if ( get_post_meta( $post->ID, 'set_to_draft_key' ) == true );
        return $post->post_status == 'draft';
set_transient( 'post_to_draft_transient', set_post_to_draft(), 60*60*12 ); // 12h interval
share|improve this answer
How do I then compare the date given in the custom field to the current date to see if the post should be turned into draft? – Phillip Cox Apr 19 '11 at 4:18
I guess the transient API (works close to Options API) does checks the timespan (3rd Arg.) and saves it to the DB. Then compares on every request the curr. timestamp against the 60*60*12 and the save to the DB. – kaiser Apr 19 '11 at 14:22
The problem appears to be that I am using Magic Fields which basically uses get instead of get_post_meta. I am not sure how to use get within the functions php. – Phillip Cox Apr 19 '11 at 17:29
Are "magic fields" part of a plugin? – kaiser Apr 19 '11 at 17:48
Yes the plugin is called Magic Fields. – Phillip Cox Apr 19 '11 at 18:01

few ways to do this task. Here i used to follow this code.

$update_post = array(
            'ID'            => $id,
            'post_status'   =>  'private',
            'post_type' =>  'job' );

Take a look at here for more info. Read Here

share|improve this answer
Please add the relevant information to your answer, instead of just linking to it - thanks. – ialocin Aug 19 at 18:04

Your Answer


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.