Skip Navigation

[Resolved] View which filters on YYYY-MM

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a custom field that holds date-like values in the format YYYY-MM. I would like to add a Query Filter to my View based on this custom field, and set it up so that the results are from the current year and the previous two years.

Solution: Use the wpv_filter_query PHP filter to find date fields "like" the 3 most recent years:

// Filter a View by date-like custom field value YYYY-MM, show only results from current year and two previous years
// https://toolset.com/forums/topic/view-which-filters-on-yyyy-mm/
add_filter( 'wpv_filter_query', 'tssupp_yyyy_mm_two_years_ago', 99, 3 );
function tssupp_yyyy_mm_two_years_ago( $query_args, $views_settings, $view_id) {
  $view_ids = array( 123 );         // your view ID or IDs
  $date_field_slug = 'field-slug';  // your date field slug
 
  // --------- you should not edit below this line ---------------------
 
  if (in_array($view_id, $view_ids)){
    $year = date('Y');
    $query_args['meta_query'] = array(
      'relation' => 'OR',
      'year1_clause' => array(
          'key' => 'wpcf-'.$date_field_slug,
          'compare' => 'LIKE',
          'type' => 'STRING',
          'value' => $year
      ),
      'year2_clause' => array(
          'key' => 'wpcf-'.$date_field_slug,
          'compare' => 'LIKE',
          'type' => 'STRING',
          'value' => ($year-1)
      ),
      'year3_clause' => array(
          'key' => 'wpcf-'.$date_field_slug,
          'compare' => 'LIKE',
          'type' => 'STRING',
          'value' => ($year-2)
      )
    );
  }
  return $query_args;
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 3 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by Christian Cox 3 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1981173
Screen Shot 2021-03-10 at 18.17.15.png

Hi,

I use a search view which displays cpt "Oliveoils", the filtering is done on several quality criteria. I need to additionally filter on custom field "production-year-month" which is of the form YYYY-MM eg 2021-03. I tried what is shown on the uploaded image, which obviously did not work as "production-year-month" is not a number. I need to somehow extract the year (YYYY) and filter using that. Any ideas as to how this can be done?

Thank you in advance,
Kostas

#1981361

Hi, I will be glad to help but I am not quite clear what you want to achieve here. Can you clarify for me?

Do you want to allow your site visitors to filter by year using a front-end filter? For example, by a dropdown filter that includes options 2021, 2020, 2019, etc., or a min/max filter for filtering by a year range?

Or, do you want to set this filter automatically, with no front-end filter, so that the results of this View will have a custom field value from 2019-2021? or some other range?

#1981395

Hi Christian, thank you for responding.

Yes, the latter. I need the search results to include only oliveoils from the current year and the two previous years. So as time goes by, older than 3 years (at most) oliveoils, should be excluded from the results.

#1981449

Hi, there's nothing exactly like that built into the Views system but I could probably help you set up a custom filter function that would give you the results you're looking for. Just to clarify let me make sure I understand how the filter should work.

If the User visits the site on January 1 2022, they should see results from 2022 and the two previous years 2021 and 2020. So that would include custom field values 2020-01 through 2022-12.

If the User visits the site on December 1 2021, they should see results from 2021 and the two previous years 2020 and 2019. So that would include custom field values 2019-01 through 2021-12.

Is that correct? If so, you can use the following filter code in your child theme's functions.php file, or in a new custom code snippet in Toolset > Settings > Custom Code, a new snippet set to run everywhere and Activated:

// Filter a View by date-like custom field value YYYY-MM, show only results from current year and two previous years
// https://toolset.com/forums/topic/view-which-filters-on-yyyy-mm/
add_filter( 'wpv_filter_query', 'tssupp_yyyy_mm_two_years_ago', 99, 3 );
function tssupp_yyyy_mm_two_years_ago( $query_args, $views_settings, $view_id) {
  $view_ids = array( 123 );         // your view ID or IDs
  $date_field_slug = 'field-slug';  // your date field slug

  // --------- you should not edit below this line ---------------------

  if (in_array($view_id, $view_ids)){
    $year = date('Y');
    $query_args['meta_query'] = array(
      'relation' => 'OR',
      'year1_clause' => array(
          'key' => 'wpcf-'.$date_field_slug,
          'compare' => 'LIKE',
          'type' => 'STRING',
          'value' => $year
      ),
      'year2_clause' => array(
          'key' => 'wpcf-'.$date_field_slug,
          'compare' => 'LIKE',
          'type' => 'STRING',
          'value' => ($year-1)
      ),
      'year3_clause' => array(
          'key' => 'wpcf-'.$date_field_slug,
          'compare' => 'LIKE',
          'type' => 'STRING',
          'value' => ($year-2)
      )
    );
  }
  return $query_args;
}

You will replace 123 with the numeric ID of this View, and replace field-slug with the slug of the date field. You should not make any other changes in the code. Delete the custom date field Query Filter you have already applied in the View editor.

Let me know if I've misunderstood what you want to accomplish.

#1981461

"If the User visits the site on January 1 2022, they should see results from 2022 and the two previous years 2021 and 2020. So that would include custom field values 2020-01 through 2022-12."

Wrong. It would include custom field values 2020-01 through 2022-01

"If the User visits the site on December 1 2021, they should see results from 2021 and the two previous years 2020 and 2019. So that would include custom field values 2019-01 through 2021-12."

Correct.

To summarise, it would include all months of current year through the current month, and all 24 months of previous two years. So depending on the current month, it would include from 25 months (if in January) to 36 months (if in December).

Hope it's clear now.

#1981831

Christian, your filter code works like a charm!

There is a small typo, but it is easily spotted, line 27 above should read:

'key' => 'wpcf-'.$date_field_slug,

A user is not allowed to create an Oliveoil in the future, so custom field values should be through current month anyway, so that is fine.

Thanks a lot for your excellent work!

Have a nice day,
Kostas

#1982405

Okay great, thanks for the feedback. I have updated the code sample to fix the error you noticed.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.