Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use this script to publish future post by server cron. USE: {site OR sub-site URL}/waw-publishing.php
<?php
/**
Description: Use this script to publish future post by server cron. USE: {site OR sub-site URL}/waw-publishing.php
Author: WeAre[WP]
Author URI: https://www.wearewp.pro
Version: 1.0
CRON
wget -O /dev/null https://site-A.com/waw-publishing.php
wget -O /dev/null https://site-B.com/waw-publishing.php
wget -O /dev/null https://site-C.com/waw-publishing.php
wget -O /dev/null https://site.com/subsite-A/waw-publishing.php
wget -O /dev/null https://site.com/subsite-B/waw-publishing.php
wget -O /dev/null https://site.com/subsite-C/waw-publishing.php
**/
if (!defined('ABSPATH')) {
require_once(dirname( __FILE__ ) . '/wp-load.php');
}
global $wpdb;
$blog_id = get_current_blog_id();
$now = time();
/** START Custom params **/
// Custom post to check
$cpt_to_scheduled = array(
'press_release'
);
// Post id to clean "blog_id" => "posts ids"
$post_id_to_clean = array(
'1' => array( 18, 25 ),
'2' => array( get_option( 'page_on_front' ), get_option( 'page_for_posts' ), 289 )
);
/** END Custom params **/
// Get posts to scheduled
foreach( $cpt_to_scheduled as $cpt ) {
$args = array(
'post_type' => $cpt,
'post_status' => 'future',
'numberposts' => 10,
);
$future_posts = get_posts( $args );
foreach( $future_posts as $future_post ) {
$post_date_gmt = strtotime($future_post->post_date_gmt);
if( $now >= $post_date_gmt ) {
// it's time to publish bro ;)
$my_post = array(
'ID' => $future_post->ID,
'post_status' => 'publish'
);
$wp_update_post = wp_update_post( $my_post, true );
// Clean WP Rocket cache for this post if WP Rocket is working
if ( function_exists( 'get_rocket_option' ) ) {
rocket_clean_post( $future_post->ID );
// Also clean WP Rocket cache for needed pages
// Check if current blog is in the array
if( array_key_exists( $blog_id, $post_id_to_clean ) ) {
// Loop to clean cache for all needed pages
foreach( $post_id_to_clean[$blog_id] as $post_id ) {
rocket_clean_post( $post_id );
}
}
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.