Assign default value to DATE field?
- This topic has 8 replies, 2 voices, and was last updated 4 years, 7 months ago by
EddyPiV.
-
AuthorPosts
-
September 11, 2020 at 4:12 PM #21822
EddyPiV
ParticipantHi,
Is there a way to define a default value to the DATE field?
What I want is to assign a date field to a cpt, make it hidden and assign a default value of today+1.Intention is in a view to skip the posts with the assigned date & time < NOW.
September 11, 2020 at 11:13 PM #21831Long Nguyen
ModeratorHi,
We can use the field setting
std
to add the default value for thedate
field. Please add this sample code to the file functions.php in the theme folder or use Code Snippets.add_filter( 'rwmb_meta_boxes', function() { $date = new DateTime('+1 day'); // here $meta_boxes[] = array( 'title' => 'Meta Box Title', 'post_types' => ['post', 'page'], 'id' => 'meta-box-id', 'fields' => array( array( 'name' => 'Date picker', 'id' => 'field_id', 'type' => 'date', // Date picker options. See here http://api.jqueryui.com/datepicker 'js_options' => array( 'dateFormat' => 'yy-mm-dd', 'showButtonPanel' => false, ), // Display inline? 'inline' => false, // Save value as timestamp? 'timestamp' => false, 'std' => $date->format('yy-m-d'), // // use the date format as js_options ), ), ); return $meta_boxes; } );
For more information, please follow the documentation.
https://docs.metabox.io/field-settings/
https://docs.metabox.io/fields/date/September 14, 2020 at 7:34 PM #21873EddyPiV
ParticipantActually, I found out that what I wanted to do wouldn't work: I was trying to set the expiration date fields of the Post Expirator plugin, but there is more needed to get it to work, probably setting a cron job.
So my question is now a bit different.
I am now skipping the posts that are older than 24 hours.I included this if statement: {% if {{ post.date }} > strtotime('-1 day') %},
but WordPress didn't like it... critical error on the website.Here is the view: https://share.getcloudapp.com/RBuOpJkA
September 15, 2020 at 3:04 PM #21878Long Nguyen
ModeratorHi,
To run a PHP function in View, please use the proxy
mb
. The code should be:mb.strtotime('-1 day')
September 15, 2020 at 3:17 PM #21880EddyPiV
ParticipantHi,
I know, the devil is in the details... I should have added the mb. prefix...
But it doesn't resolve the critical error.Deleting the "if" construction avoids the critical error, so it is really in this statement.
September 15, 2020 at 3:23 PM #21881Long Nguyen
ModeratorHi,
Two curly brackets
{{ display a value on the frontend }}
is to display a value on the frontend. Please remove it{% if post.date > mb.strtotime('-1 day') %}
.September 15, 2020 at 5:34 PM #21885EddyPiV
ParticipantThat indeed resolves the critical error.
And some new questions arise...
Question 1:
There are 2 posts found, 1 with date today, 1 with date 3 days ago.
Yet, the post.date for both (according to the view) are shown as today. Why is that???
Print of database: https://share.getcloudapp.com/X6uN152L
Result of the View: https://share.getcloudapp.com/L1uJA65q
The View-code: https://share.getcloudapp.com/xQuL2Og7Question 2:
At least 1 post should be displayed with its details, but there are no posts displayed.
Considering the post.date for both showing today, both should be displayed.
Why are no posts displayed?
Something with stored formats? How to formulate the condition correct?September 15, 2020 at 10:33 PM #21889Long Nguyen
ModeratorHi,
As I said in this topic https://support.metabox.io/topic/how-to-show-all-posts-for-specific-taxonomy/page/2/#post-21633.
With the custom query, we have to use the variable of the WP_Post object. To get the post date, use
post.post_date
. For more information, please follow the documentation https://developer.wordpress.org/reference/classes/wp_post/.September 15, 2020 at 11:05 PM #21890EddyPiV
ParticipantYes, that was it. The format mistake I already figured out.
Thanks, let's close this thread. -
AuthorPosts
- You must be logged in to reply to this topic.