User schedule a post?

时间:2016-05-05 作者:Phil

用户(非管理员)是否可以安排帖子?假设他们有一个表单,可以在其中输入一些文本,并使用日期选择器来选择要发布的帖子的日期,这将如何工作?

1 个回复
SO网友:N00b

这里有一个可能的解决方案。。

如果用户发布帖子,他/她会将其另存为草稿,当用户选择要发布帖子的日期时,会将其另存为帖子元数据,my_post_schedule_time 在我的示例中Disclaimer: 这只是一个例子。它应该工作得很好,但可能需要一些调整、调整、定制,甚至可能需要一些优化。

我想我不必告诉你们,对服务器的影响取决于你们有多少帖子。

此代码将转到新单曲.php 你应该把它放在mu-pluginsplugins 文件夹

<小时>

/*
 * Plugin Name: Your Scheduler
 * Plugin URI: http://www.your-site.com
 * Description: Schedules posts for users
 * Author: You
 * Author URI: http://www.your-site.com
*/ 



add_action( \'wp_ajax_nopriv_postSchedule\', function() {  

    // Get current time
    $now = date( \'Y-m-d H:i:s\' );

    // Get posts that has status "draft" and are "over time limit"
    $unpublished_posts = new WP_Query( array(

        \'posts_per_page\'    => -1,
        \'post_type\'         => \'post\',   // Your post type
        \'post_status\'       => \'draft\',  // Post status

        // Im pretty sure that it doesn\'t get posts without that meta value
        \'meta_query\' => array(
            array(
                \'key\'     => \'my_post_schedule_time\',
                \'value\'   => $now,
                \'type\'    => \'DATETIME\',
                \'compare\' => \'<=\',
            )
        )
    ));

    // Loop through all scheduled posts
    while( $unpublished_posts->have_posts() ) {

        $unpublished_posts->the_post();

        // Get schedule meta data
        $post_publish_time = get_post_meta( get_the_ID(), \'my_post_schedule_time\', true );

        // Format it - might not need it, it depends how you save your metadata
        $format_post_publish_time = date( \'Y-m-d H:i:s\', strtotime( $post_publish_time ) );

        // Check if we have schedule time, just in case
        if( ! empty( $format_post_publish_time ) ) { 

            // Check if that times has passed a.k.a smaller than current time
            if( $format_post_publish_time < $now ) {

                // Publish post
                wp_update_post( array( \'ID\' => get_the_ID(), \'post_status\' => \'publish\' ) );
            }
        }
    }

    wp_reset_postdata();
});
现在,您需要从主机cPanel设置服务器cron作业,该作业在设置的时间间隔内调用该函数(例如,每1小时、每2小时、每天一次等)。More often == more precise.

相关推荐