Hi, I have created a acf field to handle the date the post was published because I want to use this to filter posts in my archives.
How can I get this field to auto-populate based on the already existing posts?
Hi @kollinsayz
I’m not sure what you mean when you say “based on the already existing posts”
You could use the acf/load_value filter to pre-populate the value a field.
Here’s the PHP manual for getting the current date/time.
But remember that WordPress time stamps posts automatically, and you can use the Date Parameters of a WP_Query to show posts associated with a certain time and date period.
Since you are here, let me lay out exactly what I’m trying to do. I’d like to filter a custom post “publications” by year. But I’d like that value for “year” to come from the date the post was published.
In essence I’d like to get date post was published and from it, extract the year and add it as a meta field value for that particular posts. But I want this done automatically since I have over 400 publications and can’t really do this manually.
You can do a quick query like:
$year = date( 'Y' );
$query = new WP_Query( 'year=' . $year );
Or a date query if you want to do it the regular way:
$args = array(
'date_query' => array(
array(
'year' => date( 'Y' )
),
),
);
$query = new WP_Query( $args );
More info here: https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters