[Solved] Wpgridbuilder: filter by year

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.

1 Like

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

1 Like

Thanks a lot Patric, sometimes the answer is the most simplistic one. Taxonomy!

  • Michael
1 Like

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

4 Likes

Oh yeah!! Now that is a great solution Patric. Ill adapt it for a CPT.

1 Like

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 );

	}
}
2 Likes

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?

What is indexing?

(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.

1 Like

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

1 Like

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

2 Likes

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;
}

1 Like

NB: The field was not being updated using quick edit, please see revised and working code below:

  • Change add_action to save_post
  • In get_the_date(), replace get_the_ID() with $post_id
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 );
	}
}
1 Like