WP Future Posts are not being published - php

$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.

Related

Convert ISOdate to phpdate for ACF custom field query

I've built a custom beaver builder (wordpress) module. I'm fetching posts via Ajax. I need to query posts based off an ACF custom field date.
I am posting the date in ISO8601 format (eg 2013-12-01T00:00:00-05:00). Server side, I grab the start and end. I convert them into the format needed for the ACF query https://www.advancedcustomfields.com/resources/date-picker/
$start_date = date('Ymd', strtotime($_POST['start']));
$end_date = date('Ymd', strtotime($_POST['end']));
I run the query, and get nothing. I echo the string out, and they look correct.
If I set the date as per the example in the ACF docs - it works (code below). So I must be converting the ISOdate $_POST['start'] incorrectly. How do I convert the ISODATE so that is it something that I can use in the query?
function get_ajax_event_calendar_posts() {
$today = date('Ymd'); // this works...
$args = array(
'post_type' => array('event'),
'meta_query' => array(
array(
'key' => 'start_date',
'compare' => '<=',
'value' => $today,
),
array(
'key' => 'end_date',
'compare' => '>=',
'value' => $today,
)
),
'post_status' => array('publish'),
'posts_per_page' => 100,
'nopaging' => true,
'order' => 'DESC',
'orderby' => 'date'
);
// The Query
$ajaxposts = get_posts( $args );
//... etc
}
** edit **
.... the date stuff wasn't the problem. I was the problem... switched my compares round the right way and all works...
You haven't said what format you actually need to store the data, however you did say date('Ymd') works. Either way, use the DateTime class:
<?php
$x = new DateTime('2013-12-01T00:00:00-05:00');
echo $x->format('d/m/Y H:i:s') . "\n"; // 01/12/2013 00:00:00
echo $x->format('dmY') . "\n"; // 01122013
Here are the date formats https://www.php.net/manual/en/function.date.php

Get All WP Posts older than 15 minutes

I would like to filter the WP_Query to retrive all woocommerce order (post_type = shop_order) with particular status that are older than 15 minutes.
I.e. All posts that have been last modified on or before (now - 15 minutes ) // Hope I'm clear.
Below What I've tried
function filter_where($where = '') {
//posts in the last 15 minutes
$where .= " AND post_modified > '" . date('Y-m-d', strtotime('INTERVAL 15 MINUTE')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'shop_order_status',
'field' => 'slug',
'terms' => array('completed')
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
print_r("<pre>");
print_r($order);
print_r("</pre>");
endwhile;
However that returns all record, Seems like have to modify the $where query, how can i achieve that ?
Do you need to filter the posts_where? Can't you just use date query parameters? In your case, specifically before.
$args = array(
'date_query' => array(
array(
'before' => '15 minutes ago'
'inclusive' => true,
),
),
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
I can't test this right now, so can't verify if it would work. If you are modifying an archive then you should definitely adjust the query via pre_get_posts in lieu of creating new query.
try this change date('Y-m-d', strtotime('INTERVAL 15 MINUTE')) to date('Y-m-d H:i:s', strtotime('-15 minutes'))
function filter_where($where = ''){
//posts in the last 15 minutes
$where .= " AND post_modified > '" . date('Y-m-d H:i:s', strtotime('-15 minutes')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');

wp_insert_post auto schedule or post based on date

<?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

Gravity Forms & Advanced Custom Fields

I've successfully created the dropdown menu to auto populate the appropriate information from an Advanced Custom Field I created called 'date' on site http://albertson.staging.wpengine.com/seminars/.
Followed along with the instructions here:
http://www.gravityhelp.com/documentation/page/Dynamically_Populating_Drop_Down_Fields
The only issue I'm having is how to display the date in a "pretty" format. You can see that the date is all numbers (20140129) instead of 01/28/2014
To display the date appropriately in the seminar sections above (border red) I use:
<?php if( get_field('date')): ?>
<?php
$date = get_field('date');
// $date = 19881123 (23/11/1988)
// extract Y,M,D
$y = substr($date, 0, 4);
$m = substr($date, 4, 2);
$d = substr($date, 6, 2);
// create UNIX
$time = strtotime("{$d}-{$m}-{$y}");
// format date (November 11th 1988)
echo date('M d', $time);
?>
How do I pass this same information within the Gravity Forms Function I created to get the date to display nicely? Below is my function for Gravity Forms so far.
add_filter('gform_pre_render_4', 'populate_dates');
function populate_dates($form){
foreach($form['fields'] as &$field){
if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-dates') === false)
continue;
// you can add additional parameters here to alter the posts that are retreieved
// more info: http://codex.wordpress.org/Template_Tags/get_posts
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
$events = get_posts(array(
'post_type' => 'seminars',
'orderby' => 'date',
'order' => 'ASC',
'meta_query'=> array(
array(
'key' => 'date',
'compare' => '>=',
'value' => $currentdate,
'type' => 'DATE',
)),
'meta_key' => 'date',
));
// update 'Select a Post' to whatever you'd like the instructive option to be
$choices = array(array('text' => 'Select a Date', 'value' => ' '));
foreach($events as $post){
$choices[] = array('text' => $post->date, 'value' => $post->date);
}
$field['choices'] = $choices;
}
return $form;
}
Sounds like you're looking for PHP's date function. We convert the date string to a timestamp via strtotime() and then use date() to format the date as you wish.
$formatted_date = date( 'm/d/Y', strtotime( $post->date ) );
In your code example:
foreach( $events as $post ){
$formatted_date = date( 'm/d/Y', strtotime( $post->date ) );
$choices[] = array('text' => $formatted_date, 'value' => $post->date );
}

Wordpress XML-RPC post to a specific category

I've been trying for quite a long time and still it posts only as "Uncategorized",
In the documentation it's stated to use integer value as category ID, but that doesn't work. I've also tried writing category name as it is and in lowercase and also entering slug. According to documentation I'm doing everything right, but it still doesn't work!
wp.newPost and since it uses wp_insert_post().
public function create_post( $title, $body )
{
$title = htmlentities( $title, ENT_NOQUOTES, 'UTF-8' );
$content = array(
'post_category' => array( 18 ), // my category id
'post_type' => 'post',
'post_status' => 'pending',
'post_title' => $title,
'post_content' => $body,
'comment_status' => 'closed',
);
$params = array( 0, $this->username, $this->password, $content );
return $this->send_request( 'wp.newPost', $params );
}
Hey I've recently started using the new API.
You should use the terms_names parameter in your XML-RPC request as stated in the new docs:
http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost
Example:
Your code should be changed to look something like this.
public function create_post( $title, $body )
{
$title = htmlentities( $title, ENT_NOQUOTES, 'UTF-8' );
$content = array(
'post_type' => 'post',
'post_status' => 'pending',
'post_title' => $title,
'post_content' => $body,
'terms' => array('category' => array( 18 ) ),
'comment_status' => 'closed',
);
$params = array( 0, $this->username, $this->password, $content );
return $this->send_request( 'wp.newPost', $params );
}
I have one problem with this API method however, the Post ID is not returned only a false boolean value. Let me know if you have any luck with inserting categories and if you manage to receive the POST ID.
Thanks, this is a life saver! If you need to provide tags for your post, you can pass them in array using tags_input property, like this:
$content['tags_input'] = "action, adventure, thriller";
If you need to pass in a specific time stamp, you should use the following format
D, d M Y H:i:s +0000
and pass it using the post_date or post_date_gmt property:
$content['post_date_gmt | post_date'] = date("D, d M Y H:i:s +0000", strtotime($your_specific_date_and_time));
Hope it helps someone in the future!

Categories