Looking for assistance on how to filter posts by year. i.e 2023, 2022, 2021 and so forth
Ideally using the Facet > Filter Type: Radio or perhaps a new Facet needs to be created.
Update: See below in regards to updating fields using Quick Edit.
Looking for assistance on how to filter posts by year. i.e 2023, 2022, 2021 and so forth
Ideally using the Facet > Filter Type: Radio or perhaps a new Facet needs to be created.
Update: See below in regards to updating fields using Quick Edit.
Hi
if you donât want to get down the route of coding, you could also just create a category or term for the year and assign each post to such a category or term.
With this you can then use any standard filter facets from WPGridbuilder.
I did this for my posts.
Cheers
Patric
Thanks a lot Patric, sometimes the answer is the most simplistic one. Taxonomy!
By the way⊠if you want to go down the route of an ACF field for the year:
Put this little code snippet into the functions.php or a WPCodeBox:
<?php
add_action('acf/save_post', 'acf_save_year');
function acf_save_year($post_id) {
$the_status = get_the_date( 'Y', $post_id );
update_field('year', $the_status, $post_id);
}
Create an ACF field called âyearâ as number field in the post type.
This code snippet is then automatically updating the ACF field âyearâ with the current year (or the year that the post was created) whenever you save / update the post.
Cheers
Patric
Oh yeah!! Now that is a great solution Patric. Ill adapt it for a CPT.
If you want to have Month + Year in a ACF text field (for example âNovember 2023â change the line to this:
$the_status = get_the_date( 'F Y', get_the_ID() );
Cheers
Patric
Easy. For anyone else you can target CPT with the following:
add_action( 'acf/save_post', 'acf_save_year' );
function acf_save_year($post_id) {
$included_cpts = array( 'cpt1', 'cpt2', 'cpt3' );
if (in_array(get_post_type($post_id), $included_cpts)) {
$year = get_the_date( 'Y', get_the_ID() );
update_field( 'document-year', $year, $post_id );
}
}
Have you actually tested this with WPGridbuilder? Cause I tried to do exactly the same a few days ago and couldnât get it working with ACF - the facet was always empty, although the values were saved in the custom year field. It does work with MetaBox, though.
Yes, of course I tested it.
As a matter of fact, I use it in my homepage and on my staging sites.
Have you tried to reindex the cache in WPGridbuilder after you updated the posts or ran the code snippet?
(General FAQ - Gridbuilder á”᎟)
In order to efficiently filter content, each facet (filter area) are indexed in an custom index table. It means, that everytime you create a facet, the plugin fetches all facet values according to your facet settings. This process is called indexing and simply allows to âprefetchâ results and to store them in a simple and indexed table.
Cheers
Patric
I did try reindexing and it didnât help. Whatâs funny is that I just tried again and it works. The only difference I can find is in the hook - I used âsave_postâ the first time around and now I used âacf/save_postâ. It really shouldnât matter as the data was saved correctly in both cases. Possibly a WPGB hiccup.
Hi
I think itâs rather an issue from the ACF update routine as the hook must be with acf/save_post.
If you still have problems, try saving the data with this line instead:
update_post_meta( $post_id, âfieldnameâ, $year);
Cheers
Patric
I doubt it. As I said, the data was stored correctly.
Anyway, what I like in MetaBox is that you can make fields like these hidden. I did just discover the .acf-hidden class that you can use in ACF, though.
Yes, you can simply add hidden into the class field in the presentation tab in the acf field editor:
Cheers
Patric
Hi
just because this is so much funâŠ
Here is the version that only writes the data into the acf field if the acf field is empty:
<?php
add_action('acf/save_post', 'acf_save_year');
function acf_save_year($post_id) {
$included_cpts = array( 'cpt1', 'cpt2', 'cpt3');
if (in_array(get_post_type($post_id), $included_cpts)) {
$check_acf_field = get_field('document-year', $post_id);
if (empty($check_acf_field)) {
$the_year = get_the_date( 'Y', $post_id );
update_field('document-year', $the_year, $post_id);
}
}
}
Cheers
Patric
Alternative option to hide a field: Use an Admin only toggle, which is a bit more visible. Both are good solutions.
/**
* Adding Custom Settings to Fields
*
* @ ACF Admin Only Field
* @ https://www.advancedcustomfields.com/resources/adding-custom-settings-fields/
*
*/
add_action( 'acf/render_field_settings', 'prefix_admin_render_field_settings' );
function prefix_admin_render_field_settings( $field ) {
acf_render_field_setting( $field, array(
'label' => __( 'Admin Only?', 'my-textdomain' ),
'instructions' => '',
'name' => 'admin_only',
'type' => 'true_false',
'ui' => 1,
), true ); // If adding a setting globally, you MUST pass true as the third parameter!
}
add_filter( 'acf/prepare_field', 'prefix_admin_prepare_field' );
function prefix_admin_prepare_field( $field ) {
// Bail early if no 'admin_only' setting or if set to false.
if ( empty( $field['admin_only'] ) ) {
return $field;
}
// Prevent field from displaying if current user is not an admin.
if ( ! current_user_can( 'manage_options' ) ) {
return false;
}
// Return the original field otherwise.
return $field;
}
NB: The field was not being updated using quick edit, please see revised and working code below:
add_action( 'save_post', 'acf_save_year' );
function acf_save_year($post_id) {
// Array of post types
$included_cpts = array( 'document', 'cpt2', 'cpt3' );
// Check array against post_id and update field
if (in_array(get_post_type($post_id), $included_cpts)) {
$year = get_the_date( 'Y', $post_id );
update_field( 'document-year', $year, $post_id );
}
}