Skip Navigation

[Resolved] How to set default value for post field ==> field type "Date"

This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 19 replies, has 2 voices.

Last updated by Engedi 5 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#909279

Hi Support, first of all, I will tell you what I want to achieve.

I want to show a list of custom post from custom post type "Offer" in the wordpress archive. So I use LAYOUT wordpress archive to do so.

For the post type "Offer", I associate it with a Post Field Group "Promotion Details".

In the post field group, I have a post field "Expiry Date". It is a field type "Date".

When a user add the "Offer" post, he will choose the Expiry Date. Since it is not mandatory to choose a date, that "Expiry Date" field can be empty.

On the "Offer" wordpress archive, I want to filter the listing based on Expiry Date. It should only show the posts which are not expired and also the posts without Date filled by the user (Empty).

My problem is that I cannot show the posts with the empty Date.

How can I achieve following:
1. Show a listing with no date (empty Expiry Date) + Expiry date greater or equal to today date.

Please enlighten me. Thank You very much.

#909421

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Thank you for contacting our support forum.

The best way I see this being done is for you to allow the users to set the expiry date by making the field mandatory. The reason is because if we set a fixed expiry date using code then eventually all posts created will be expired and the code would need to be updated.

Let me know what you think of this.

Thanks,
Shane

#909456

Hi Shane,

What you suggested is the easiest approach. But sometimes, the OFFER expiry date is unknown to the user as well. That's why by not setting it as mandatory is a better option.

Is there any way that I can set the expiry date default value as a string (not a number as it is when user input a date) and stored in the database?

#909528

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

So sometimes they will set a date and sometimes they wont correct?

You're not able to set it as a string since the date is actually stored as a timestamp but could you let me know a little more on this number ?

When you are printing it out is it that you get a number or do you get the actual date.

Please let me know.
Thanks,
Shane

#909539

I set 3 conditions, so 3 types of output.

1. If there is no time, the message will be: Expiry Date: Soon
2. If the expiry date is larger than or equal today, the message will be: Expiry Date: The Date
3. If the expiry date is smaller than Today, the message will be: Expiry Date: Promotion Ended.

These are the code i used:

[wpv-conditional if="( $(wpcf-expiry-date) gte 'TODAY()' )"]
<div class="expirydate"><p>Expiry Date: [types field='expiry-date'][/types]</p></div>[/wpv-conditional]

[wpv-conditional if="( $(wpcf-expiry-date) lt 'TODAY()' ) AND ( $(wpcf-expiry-date) ne '' )"]
<div class="expirydate"><p>Expiry Date: Promotion Ended</p></div>[/wpv-conditional]

[wpv-conditional if="( $(wpcf-expiry-date) eq '' )"]
<div class="expirydate"><p>Expiry Date: Soon</p></div>[/wpv-conditional]

So, i want to be able to show both 1 and 2 in the archive listing. I know that when there is no default value for the expiry date, there is nothing stored in the database. The query filter cannot filter something which is not in the database. Hence i think perhaps it will be good if I can set a default value for the expiry date which is not in the timestamp value, eg a string.

#909572

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

You wouldn't be able to use a string to set the default but your can set a timestamp.

Is this being created by users on the Frontend using the Toolset Forms ?

If so then for the value attribute for the Form you can set the value as a timestamp. For a timestamp generator you can take a look at the link below.

hidden link

So you will need to use the timestamp 1528316586

Please let me know if this helps.

Thanks,
Shane

#909652

it is not for the front-end form. It is for back-end when my user creates the post. I dun see any option to set default value for the date. Is there any custom code or hack?

#910771

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

I was able to write up some custom code that should at least set the date for you.

Add the following to your functions.php file

function my_custom_fonts() {
  echo '<script>
    var now = new Date();

var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);

var today = now.getFullYear()+"-"+(month)+"-"+(day) ;
jQuery(document).ready(function() { 
jQuery(".js-wpt-date").val(today);
});
  </script>';
}

Please let me know if this helps.
Thanks,
Shane

#910780

I added the code to my functions.php. I get following message:

Your PHP code changes were rolled back due to an error on line 50 of file wp-content/themes/boombox-child/functions.php. Please fix and try saving again.

syntax error, unexpected '&'

and i lost everything from the functions.php, it means everything becomes empty. Got to find out what was the code I put there before.

#910783

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

It seems i forgot to send you the add_action section.

add_action('admin_head', 'my_custom_fonts');

function my_custom_fonts() {
  echo '<script>
    var now = new Date();

var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);

var today = now.getFullYear()+"-"+(month)+"-"+(day) ;
jQuery(document).ready(function() { 
jQuery(".js-wpt-date").val(today);
});
  </script>';
}

This code should work now. Not sure why its throwing an error on your side but could you try this one.

#910804

Hi Shane,

Yes, it shows today date in the form. But that is the only thing it does. It did not save that value to the database when i save the post.

Btw, if you can make it to save to the database, can you set a date not today? Maybe something like 20 January 1981?

#911284

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

I'm looking on this to see if I can get this working as intended.

Thanks,
Shane

#911290

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

It seems I would need to set this directly in the database when the post is submitted with an empty datepicker.

Could you use this code instead?

function set_date_field( $post_id ) {
	$date = get_post_meta($post_id, 'wpcf-datepicker');
	if(empty($date)){
		update_post_meta($post_id, 'wpcf-datepicker','443404800');
	}
}	
add_action( 'save_post', 'set_date_field' );

You will need to replace wpcf-datepicker with the slug of your datepicker field including the wpcf- prefix attached to the slug.

Thanks,
Shane

#911983

Thank you for the code. Sadly it did not save the data into the database. Can you please try again?

#912009

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

It would for me, this code is meant to update the database so it should work.

Could you send me a screenshot of what you added to your functions.php file?

Thanks,
Shane

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.