2

In a previous question I needed to set a future dated post (custom post type) as published on save instead of scheduling it.

On the date it's 'scheduled' for I'd like to set it as a draft again. I've been trying to hook into init and wrap it in an if checking for post type. I figured it would compare to server time and then set status to draft if it was older than server time.

As a rough outline:

if custom post type
  get_the_time of post
  get the server time
  if post time is older than server time set status to draft
endif

Here and update with some code I'm working with.

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' );

It's getting the posts I need and is giving me the server time but isn't doing anything past that as far as I can tell.

4
  • curtismchale, I'd suggest adding a clear and definite question to your post (as well as making your title a question). As it is, your intentions are somewhat clear, but could be made more so. Sep 7, 2010 at 19:06
  • So basically you want to set expiration dates for your custom post type rather than publication dates? I can see the purpose of this kind of setup ... but I think you're depending too much on the standard post publication time. Give me some more time to think, but I think you'd be better suited with a custom field for a visibility timeout than a direct tie in to the post publication date.
    – EAMann
    Sep 7, 2010 at 20:41
  • I've seen (and can do) timeouts based on a modified loop and custom field. There is already lots of custom write panels and fields in the custom post type (multiple music artist, twitter for artist...) that we'd like to avoid editing anymore data. Also setting the date twice seems a bit redundant. Sep 7, 2010 at 21:29
  • I think you should store expiration dates in custom fileds or meta boxes and add a WP cron job to clean up expirated posts. This would leave the usual WP flow intact.
    – Raphael
    Jan 18, 2012 at 10:48

2 Answers 2

1

Your query isn't giving you a post ID, it's giving you an entire post. The SELECT * returns all columns, ID, post_status, etc. So setting $my_post['ID'] = $a doesn't do anything for you in this case.

Try using: $my_post['id'] = $a->ID; instead. That should accurately set your ID before you call wp_update_post() and you should be in business.

Aside from that, I see no problems with what you're doing ... except that querying the database every time the site is loaded might eventually create performance issues. I'd set up an hourly chron job instead to automate the process ... then it doesn't depend on or slow down user traffic.

2
  • Yeah I wondered about the DB hits. Now that it works I can play with cron. There was one issue still with my code (now corrected). I wasn't passing a post_id to get_the_time so it would just grab every post. Sep 7, 2010 at 21:59
  • @curtismchale, is it possible to share the complete solution of this? i have make the same question and i haven't found a working solution yet. Thanks
    – Philip
    Jan 22, 2011 at 17:41
-1

I wanted to comment but looks like your post is closed for comment.

Anyway, be very careful, my friend, to leave the declaration of array OUTSIDE of the loop:

$my_post = array();

otherwise, your script will use excessive memory and never finish for large database!

Let me know if any questions

2
  • Could someone please tell me that I'm wrong and that's why I got a downvote?
    – ericn
    Nov 21, 2012 at 13:28
  • Could you explain how declaring an array will use excessive memory? Oct 11, 2013 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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