I have Field Group “nimipäivät” (namedays)
Inside that I have 2 fields
“nimi” (name) = text
“päivämäärä” (date) = date picker
IMAGE:https://ibb.co/Gx8Z7YS
Also I have Custom post plugin and there is added namedays
IMAGE: https://ibb.co/6X79X4F
Group fields post rule is set to nimipäivät IMAGE:https://ibb.co/Gx8Z7YS
Now I would need to show todays date posts ONLY on my homepage.
I have this code added to my funtions.php
function show_todays_name_days() {
// Hae tämän päivän päivämäärä
$today = date(‘Ymd’); // Muutettu takaisin Ymd-muotoon
// Luo uusi WP_Query-objekti
$args = array(
‘post_type’ => ‘nimipaivat’,
‘meta_query’ => array(
array(
‘key’ => ‘field_65236d64b874e’, // Päivitä tämä kentän avaimellasi
‘value’ => $today,
‘compare’ => ‘=’
)
)
);
$query = new WP_Query($args);
// Tarkista, onko postauksia
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// Tulosta postauksen otsikko ja sisältö
echo ‘<h2>’ . get_the_title() . ‘</h2>’;
echo ‘<div>’ . get_the_content() . ‘</div>’;
}
} else {
// Ei postauksia tälle päivälle
echo ‘Ei nimipäiviä tänään.’;
}
// Palauta alkuperäinen postausdata
wp_reset_postdata();
}
add_shortcode(‘show_todays_name_days’, ‘show_todays_name_days’);
And I am using this shortcode : [show_todays_name_days]
But this code doesnt work and I dont know how to show todays date posts on my homepage.
Using also Elementor with Hello theme
I have Field Group “nimipäivät” (namedays)
Inside that I have 2 fields
“nimi” (name) = text
“päivämäärä” (date) = date picker
IMAGE:https://ibb.co/Gx8Z7YS
Also I have Custom post plugin and there is added namedays
IMAGE: https://ibb.co/6X79X4F
Group fields post rule is set to nimipäivät IMAGE:https://ibb.co/Gx8Z7YS
Now I would need to show todays date posts ONLY on my homepage.
I have this code added to my funtions.php
function show_todays_name_days() {
// Hae tämän päivän päivämäärä
$today = date(‘Ymd’); // Muutettu takaisin Ymd-muotoon
// Luo uusi WP_Query-objekti
$args = array(
‘post_type’ => ‘nimipaivat’,
‘meta_query’ => array(
array(
‘key’ => ‘field_65236d64b874e’, // Päivitä tämä kentän avaimellasi
‘value’ => $today,
‘compare’ => ‘=’
)
)
);
$query = new WP_Query($args);
// Tarkista, onko postauksia
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// Tulosta postauksen otsikko ja sisältö
echo ‘<h2>’ . get_the_title() . ‘</h2>’;
echo ‘<div>’ . get_the_content() . ‘</div>’;
}
} else {
// Ei postauksia tälle päivälle
echo ‘Ei nimipäiviä tänään.’;
}
// Palauta alkuperäinen postausdata
wp_reset_postdata();
}
add_shortcode(‘show_todays_name_days’, ‘show_todays_name_days’);
And I am using this shortcode : [show_todays_name_days]
But this code doesnt work and I dont know how to show todays date posts on my homepage.
Using also Elementor, Hello theme
The two fields are Date Picker fields. Both field have a value.
Hey!
I have deleted all as what I tryed didnt work so there no fields rn.
So my issue is how to get data if I create Field Group Labels:
Field Group: Namedays
Labels:
Date (date picker)
Name (text)
With these I can create a nameday post.
Now on my homepage, I would need to get data from that “Date” so that it shows only the posts that have todays date on it. By that way the post (someones dayname) appears on my homepage.
How to do this? If this makes sense to you, please let me know step by step guide as I havent used ACF before.
Note: I use Elementor, and If I now set Elementor widget to show “nameday” posts, it will show all of them. It should only show the posts that has todays date on them.
I am not an ACF expert at all. I am just learning it, too. There is some great support, in my experience, here but I suggest you try to explain what you have tried thus far.
One idea is to make open hours and day a repeater field? You could have a dropdown to select the day of the week, and a text box for open time and a text box for close time in 24 hour format, for example. You could also use a time picker to ‘from’ and ‘to’ instead of input boxes. This way you could have several entries for each day (eg. one entry: Monday 0900 1200, next entry: Monday 1300 1700).
Once you have above working, you would just grab the current time and day.
$day = date('w', $timestamp); // just one example
Above would return a number from 0 to 6. 0 is Sunday and 6 is Saturday.
Then you would loop the repeater field to find that day if it existed.
Then you would grab the time. Lots of ways to do that.
Then you would loop the repeater field to see if it falls between from/to.
If true, it is open and you just need to calculate the current time to the next close time and then maybe the next open time. If false, you would search for the next open day and time and calculate the time.
Maybe you could add another field to log national holidays or other closed days. It could be a date with a input field for comments. The first loop would test to see if today was a national holiday or a store closer and echo the comment field could mention the reason why the store was closed.
Just ideas. Hope this helps. Try to get the first step of capturing the day, from-time and to-time in a repeater field.
For the JS to work you’ll need to enqueue after loading ACF.
wp_enqueue_script( 'date-picker-js', get_stylesheet_directory_uri() . '/js/custom_date_picker.js', array('acf-input'), '1.0.0', true );
You need to add custom JS and use the acf JS API to adjust the date_picker_args.
Hello @svsdesign. I had to dig through the plugin files to find this, however, here are the ones I found/needed to remove:
acf_update_setting('enqueue_select2', false);
acf_update_setting('enqueue_datepicker', false);
acf_update_setting('enqueue_datetimepicker', false);
acf_update_setting('enqueue_google_maps', false);
You would need to use the ACF JS API hook time_picker_init.
Sorry, that’s really all the information I have. I am not familiar with the time picker settings.
Alternately you can use the acf/prepare_field hook. The date picker field does not have a default value. You would need to set $field['value']
to the default value, for example if the value is empty then set it to the default.
Thanks for the inputs, I did something like what John purposed at first but then I did solve this myself with standard jQuery. I simply added a class to the field, “datepick” and then loaded a jQuery datepicker with this code in the wordpress admin.
jQuery(document).ready(function(){
jQuery( ".datumpick .acf-input input[type=text]" ).datepicker();
});
Its not the ACF datepicker but it is just like it and works just as I want. I can add a date with the datepicker or I can write any word or sentence.
Hope it may help someone in the future as well.
What @wpfieldwork says is a good start. From a user UI perspective I would probably have a radio field to select entering an exact date or “other” value and then have the date picker and text fields be conditional on which is chosen and mark them as required. Only the field shown would be required. Also I’d probably put all of these fields inside a group field to make the UI more understandable.
I’ve usually done this with a date picker field that isn’t required then an alternative free text field. On the front end, I’ve then had it checking for whatever should be the priority first then falling back to the other field type. Then in the backend on the prioritized field, I usually note in the instructions that if the field is populated, it will take priority over the other.
So maybe you have a text field that is called something like Starting Date and then a date picker field called Exact Start Date and you note that if the exact start date is populated, it will take priority over the starting date text field (in case someone populates both and you only want one)
Something like:
$exactDate = get_field('exact_start_date');
if ( $exactDate ) {
echo $exactDate;
} else {
// the text field
the_field('starting_date');
}
The ACF date picker field saves values to the database in “Ymd” format. This is not a correct date time format for use with the ACF post date field. You need to get the value saved by ACF using acf functions and/or convert the value returned to a date/time value in the format “Y-m-d H:i:s”.
hi John. the problem, i’ve learned, is that ACF returns the current date if using strtotime and date_i18n. i’ve tested running a plain vanilla event_date_end and there’s nothing there, but as also reported here, this isn’t always the case!
if i use your IF statement, is the rest of it basically correct?
I put this code in my functions file:
add_action('acf/save_post', 'test_update_post_date_from_acf', 20);
function test_update_post_date_from_acf($post_id) {
// remove this filter to prevent potential infinite loop
remove_filter('acf/save_post', 'test_update_post_date_from_acf', 20);
// date format must be "Y-m-d H:i:s"
$post_date = get_field('email_scheduled_date');
$post = wp_update_post(array(
'ID' => $post_id,
'post_date' => $post_date));
}
And I made sure that my datepicker return format is set to: Y-m-d H:i:s
I know this is an old post but I am trying to publish a front-end post with a future date using a date picker field and nothing I am finding is working.
Perhaps there is a newer way to get this done here nearing 2023?
I cannot manage to get this working whatsoever…. hopefully someone can assist. Perhaps things have changed in the last 8 years and there is a better way to do this?
Here’s my code to display my form:
<?php acf_form(array(
'post_id' => 'new_post',
'id' => 'new-email',
'new_post' => array(
'post_type' => 'emails',
'post_status' => 'future',
'field_groups' => array(1091),
'post_title' => true
),
'return' => '',
'submit_value' => 'Add new email'
)); ?>
Not sure if I have the field_groups ID correct or not. Not sure how to retrieve that. Is that the “post id” of the field group?
Here’s my functions file code:
add_filter('acf/pre_save_post' , 'emails_pre_save_post', 10, 1 );
function emails_pre_save_post($post_id) {
// check if this is to be a new post
if ($post_id != 'new_emails') {
return $post_id;
}
// The date picker field
// change field_563657f49b4d9 to the field key of your date field
$postdate = date('Y-m-d H:i:s', strtotime($_POST['acf']['field_emails_email_scheduled_date']));
// Create a new post
$post = array(
'post_type' => 'emails',
'post_title' => $_POST['acf']['_post_title'],
'post_status' => 'future',
'post_date' => $postdate,
'edit_date' => true
);
// insert the post
$post_id = wp_insert_post( $post );
// return the new ID
return $post_id;
}
Hello @hube2,
Thanks again for taking time for me and my issue…
I’m sorry, I think I’m not explaining myself well.
I would like to display first the posts (CPT : films) that have a date (acf date picker field : date_de_sortie) and then those that do not.
Currently they display first those without a date.
I made a photomontage to explain what I’m looking for (I framed date on it to see where date is).
Here is the photomontage
All the best.
Pierre
Example for JS:
(function( $ ) {
'use strict';
acf.add_filter('date_picker_args', function(args, el) {
args.yearRange = "-1000:+50";
return args;
});
})( jQuery );
Me neither… I don’t understand.
Yes my code is running.
I tried running it with :
//$query->set( 'tax_query', $taxquery );
It doesn’t interfer.
The field ‘date_de_sortie’ is my ACF date picker field.
If I set ‘date_clause’ => ‘DESC’, it effectively sorts posts in the opposite direction which is logical, but always posts without any ‘date_de_sortie’ are in first.
Hope I’m clear in my explanations ;-).
Best.
Pierre
I do not know if that is a set-able argument for the date picker. See this for information https://www.advancedcustomfields.com/resources/javascript-api/#filters-date_picker_args
Hi, Did you find a solution for this? I’m also using Display Format m/d/Y for a date picker field. I’m able to get it to display in a Gravity Forms text field fine but its in Ymd format. I cannot get it to display in a Gravity Forms date field at all. Thanks.
Stumbled upon this report after a couple of hours struggling, can confirm the same issue with WP 6.01 / ACF 5.12.3
I’m using ACF with a calendar PHP script and I need to use
date_default_timezone_set(‘Europe/Rome’);
in one of my functions.
This results literally in a datepicker value that, when retrieved from that function
get_field (‘my_date’, $post_id); //format Y-m-d
// * * * output: 2022-07-06`
while if edited in the original post field or even in the database record is:
// * * * output: 2022-07-07
EDIT: as per this other thread, it looks like a WordPress core issue
This fellow explains how to use javascript to restrict the dates of the DatePickers, just used it in a project of mine:
https://www.damiencarbery.com/2021/10/acf-date-time-picker-set-end-date-to-after-start-date/
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.