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.
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.
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?
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.
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.
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?
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>';
}
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.
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.