<?php
//today date 2014-07-27
$title="title";
$content="content";
$date="2014-07-30";
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'post',
'post_date' => $date,
'post_author' => 1
);
$post_id = wp_insert_post( $my_post );
?>
how to make post auto post when the date today (or older day) and auto schedule when the date tomorrow ?
ps: for today (or older day) maybe that code will work correctly but how to do it for auto schedule when i put the date tomorrow or next day (if it possible please give sample code)
update question: what i mean is how to make wp_insert_post make a schedule post when the date set to the future (next day/next month/next year or specific date) because when i try to set 'post_date' => "2015-08-30" the post keep post with today date what i want is that post will create as schedule post
thanks
To test if the date is for tomorrow, try something like this :
$tomorrow = date('Y-m-d', strtotime('tomorrow'));
And for the day after tomorrow :
$day_after_tomorrow = date('Y-m-d', strtotime('tomorrow + 1 day'));
Then you can test $date:
if ($date == $tomorrow) {
echo "tomorrow";
} elseif ($date == $day_after_tomorrow) {
echo "dayaftertomorrow";
}
Updated answer :
Basically post_date is for the time post was made and if you want hack i think that you need to have date in this format : [ Y-m-d H:i:s ]
The post datetime is [ Y-m-d H:i:s ], try it:
<?php
$timezone_offset = get_option( 'gmt_offset' );
$post_date = date_create( "2014-07-30" );
$post_date_gmt = date_create( "2014-07-30" );
$post_date_gmt->add( new DateInterval( "PT{$timezone_offset}H" ) );
//today date 2014-07-27
$title="title";
$content="content";
$date="2014-07-30";
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'post',
'post_date' => $post_date->format('Y-m-d H:i:s'),
'post_date_gmt' => $post_date_gmt->format('Y-m-d H:i:s'),
'post_author' => 1
);
$post_id = wp_insert_post( $my_post );
?>
Enjoy your code
Related
I am having issues with the functionality of my code. The custom post should expire after a certain number of days. At the moment it’s set to 3 days just so I can test it.
But the posts are expiring even though the expiry date is before today's date, and I can’t figure out why?!
// Setup Cron Job Function to expire posts and send out emails.
function listing_expiry_date() {
// Get the current date
date_default_timezone_set('Europe/London');
$today = date('m/d/Y');
// Custom Post Type for listings, grab published posts
$args = array(
'post_type' => array( 'post_type_listings' ),
'post_status' => array( 'Publish' ),
'posts_per_page' => -1,
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
// Get the listing ID
$post_id = get_the_ID();
// Get listing post published date (not pending date)
$published_date = get_the_date( 'm/d/Y', get_the_ID() );
$expiry_date = date( 'm/d/Y', strtotime( '+3 days', strtotime($published_date) ) );
// If todays date is equal to or greater than the expiry date...
if ( ($today == $expiry_date) || ($today > $expiry_date) ) :
// change post status to 'expired'
$postdata = array(
'ID' => $post_id,
'post_status' => 'expired',
);
// Update post data
wp_update_post($postdata);
endif;
endwhile; //endwhile The Loop
endif; //endif The Loop
}
IT seems your issue would be how you're evaluating the dates. In this case, it would be best to use a Unix timestamp to evaluate which is greater.
function listing_expiry_date() {
// Get the current date.
date_default_timezone_set( 'Europe/London' );
$today = date( 'U' );
// Custom Post Type for listings, grab published posts.
$args = array(
'post_type' => array( 'post_type_listings' ),
'post_status' => array( 'publish' ),
'posts_per_page' => -1,
);
// The Query.
$query = new WP_Query( $args );
// The Loop.
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
// Get the listing ID.
$post_id = get_the_ID();
$expiry_date = date( 'U', strtotime( '+3 days', get_the_date( 'U', $post_id ) ) );
// If Expiration date is < or = today.
if ( $expiry_date <= $today ) :
// change post status to 'expired'
$postdata = array(
'ID' => $post_id,
'post_status' => 'expired',
);
// Update post data.
wp_update_post($postdata);
endif;
endwhile; // endwhile The Loop.
endif; // endif The Loop.
}
I found that code here and I'm wondering how can I add example: if order created time > order created time + 5 minutes?
Now code just check today datetime and order created datetime and make datediff, but I want to make time diff.
If someone can help me, thanks already!
function myplugin_cancel_unpaid_wc_orders() {
global $myplugin_options;
$my_cancel_time = $myplugin_options['myplugin_cancel_time'];
//$date = date( "Y-m-d H:i:s", strtotime( '-' . absint( $myplugin_cancel_time ) . ' MINUTES', current_time( 'timestamp' ) ) );
$query = ( array(
'limit' => 5,
'orderby' => 'date',
'order' => 'DESC',
'status' => array( 'pending' )
) );
$orders = wc_get_orders( $query );
foreach ( $orders as $order ) {
$date = new DateTime( $order->get_date_created() );
$today = new DateTime();
$interval = $date->diff($today);
$datediff = $interval->format('%a');
if ( $datediff >= 4 ) {
$order->update_status('cancelled', 'Cancelled for missing payment');
}
}
}
Convert the order created date to seconds. 5 minutes equal 300 seconds.
$order_created_date = $order->get_date_created(); // Get order date created WC_DateTime Object
$order_created_seconds = $order_created_date->getTimestamp(); // Get order create date in seconds
$end_time = $order_created_seconds+300; // Order created time + 5mins
$current_time = time();
if ($current_time >= $end_time) { // Check if end time
$order->update_status('cancelled', 'Cancelled for missing payment');
}
I am trying to filter (show) Gravity Forms Entries on the front end of my site between two certain dates. One being today (the current present date) and a custom date added within the backend of the website.
So only entries that were submitted between the custom date and the present date.
I have two variables storing these dates:
$new_start_date is the custom date, and
$end_date is the present days' date.
My code is outputting 200 entries but some of which are before the custom start date.
Below is my query code:
<?php
//Get the Form ID to get the entries from and store it within a varibale
$form_id = GFAPI::get_form(2);
// Get Dates
$date_page_id = get_field( 'update_dates_page_link', 'options', false, false );
$start_date = get_field( 'oak_submission_date', $date_page_id ); // date of the last submission date
$end_date = date( 'Y-m-d', time() ); //get today's date
// Convert start_date to match end_date format
$new_start_date = DateTime::createFromFormat( 'd/m/Y', $start_date );
$new_start_date = $new_start_date->format( 'Y-m-d' );
$search_criteria = array(
'status' => 'active',
'start_date' => $new_start_date,
'end_date' => $end_date,
'field_filters' => array(
array(
'key' => '53', // Trust name field
'value' => 'Oak Trust',
),
array(
'key' => '49', // Grant Made field
'value' => 'No',
),
),
);
$sorting = null;
$paging = array( 'offset' => 0, 'page_size' => 200 );
$entries = GFAPI::get_entries( $form_id, $search_criteria, $sorting, $paging );
// Code that outputs to the screen
foreach ( $entries as $entry) :
echo '<li>';
echo 'ID: '. $entry[68] .' : ' . $entry[2] .' : Submission Date: '. $entry[54];
echo '</li>';
endforeach;
GFAPI::get_form(2) doesn't return the Form ID it returns the Form. Try this:
$entries = GFAPI::get_entries( 2, $search_criteria, $sorting, $paging );
$date = date("Y-m-d H:i:s");
$date = strtotime("+60 minutes");
$date = date("Y-m-d H:i:s", $date);
$postId = wp_insert_post(array(
'post_author' => 1,
'post_category' => $kategori,
'post_content' => $aciklama,
'post_status' => 'future',
'post_date' => $date,
'post_date_gmt' =>$date,
'post_title' => $adi,
'post_excerpt' => '',
'post_type' => 'post',
'tags_input' => $etiketler
));
I have written the code above. It inserts post to the database and i see the post as scheduled in the article list. But It doesn't publish when time comes(now + 60minutes). What am I missing? Should I write anything else to make it publish when time comes?
Try this date from WordPress function,
$cur_date = current_time('mysql');
$date = date("Y-m-d H:i:s", strtotime($cur_date) . "+60 minutes");
May be help you.
I'm trying to display a list of posts in Wordpress with dates that are equal to or greater than today - the purpose is to list out upcoming events.
Here's the code I have right now:
// Get the current date
$current_date = date('M d, Y');
$current_date = strtotime( $current_date );
// Get the event date
$post_date = get_the_time('M d, Y');
$post_date = strtotime( $post_date );
query_posts(array('category_name' => 'events',
'meta_query' => array(
array(
'key' => $post_date,
'value'=> $current_date,
'compare'=>'>='
)
),
'showposts' => 4,
'orderby' => 'date',
'order' => ASC));
while (have_posts()) : the_post();
As you can see, I'm grabbing the current date and the date of the post. I know this code works, because I had it functioning mostly in the following format:
// Get the current date
$current_date = date('M d, Y');
$current_date = strtotime( $current_date );
query_posts(array('category_name' => 'events',
'showposts' => 4,
'orderby' => 'date',
'order' => ASC));
while (have_posts()) : the_post();
// Get the date
$post_date = get_the_time('M d, Y');
$post_date = strtotime( $post_date );
// If older than current date, don't show it
if( $post_date >= $current_date ):
But the problem with that is that it finds the posts, then compares them to the current date. So if I want to show 4 of my 10 posts, but 3 are hidden because they're in the past, I'm actually only displaying 1 post here.
I need to compare to the current date then show 4 posts from the result of that calculation.
Any help is greatly appreciated. Thanks!
To do this you can use a date_query as part of your query_posts() call (in place of the meta_query).
This will then eliminate the need to check the date of Posts once a query has been run, so you should always get the four that you are seeking.
$today = getdate();
$args = array(
'date_query' => array(
array(
'year' => $today["year"],
'month' => $today["mon"],
'day' => $today["mday"],
'compare' => '>=',
),
),
'posts_per_page' => 4,
'orderby' => 'date',
'order' => ASC
);
query_posts($args);
Note: I'd strongly recommend reviewing the WP_Query codex for further information as there are some very useful parameters in there. These can help you to further refine the Posts that are returned and include the likes of post_type, post_status and cat. Of course not all of these will be relevant to you in all situations (or possibly at all), but it's still worth a read.
Caution: Please be aware that posts_per_page replaced show_posts some time ago.
Update
You mention in the comments that the above code is working, but only one Post is being retrieved. My initial thoughts are that this is caused by one of two things -
You only have one future Post, and thus there are no more to show.
Something is hooking your query and changing the LIMIT portion.
I suggest viewing the query that was passed to MySQL as soon as it is made. To do this, add the following line directly below query_posts($args) and reload your page. -
global $wpdb;
echo '<pre>'; print_r($wpdb->last_query); echo '<pre>';