Skip Navigation

[Resolved] Filtering results by current day of the week, not date

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

Problem: I have a taxonomy that includes terms for each day of the week. I would like to filter posts in a View using the current day of the week, so that only posts tagged with the term for today are shown.

Solution: Taxonomy term filters do not allow any date criteria in wp-admin, so custom code is required. Assuming the term slugs are "monday", "tuesday", and so on through "sunday", add the following code in your child theme's functions.php file to filter a View by term based on the local current day of the week:

add_filter( 'wpv_filter_query', 'only_todays_posts',99,3 );
function only_todays_posts( $query_args,$views_settings, $view_id) {
  if ($view_id == 12345){
    $query_args = array(
      'tax_query' => array(
        array(
          'taxonomy' => 'day-of-the-week',
          'field'   => 'slug',
          'terms' => strtolower(date('l')),
          'operator' => 'IN'
        ),
      ),
    );
  }
  return $query_args;
}

Replace 12345 with the numeric ID of your View, and replace day-of-the-week with the slug of your custom taxonomy. Nothing extra is needed in wp-admin.

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

This support ticket is created 6 years, 10 months 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 2 replies, has 2 voices.

Last updated by CarrieI2136 6 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#621751

Hello! I am trying to create a View that will filter by a taxonomy that is the day(s) of the week. I want the view to show the posts where the taxonomy matches the current day of the week.

For example, I have a page of kids eat free deals in our town: hidden link Each of those posts has a taxonomy selected of Monday, Tuesday, Wednesday, etc. I would like to put a widget on the home page that shows "Today's Kids Eat Free Deals" and if it's Monday, it just shows the posts that have the Monday taxonomy.

I'm familiar with how to do this by *date* but not by day of the week. Can you please help me?

#621882

Hi, unfortunately taxonomy term filters do not allow any date criteria in wp-admin, so custom code is required. Assuming the term slugs are "monday", "tuesday", and so on through "sunday", add the following code in your child theme's functions.php file to filter a View by term based on the local current day of the week:

add_filter( 'wpv_filter_query', 'only_todays_posts',99,3 );
function only_todays_posts( $query_args,$views_settings, $view_id) {
  if ($view_id == 12345){
    $query_args = array(
      'tax_query' => array(
        array(
          'taxonomy' => 'day-of-the-week',
          'field'   => 'slug',
          'terms' => strtolower(date('l')),
          'operator' => 'IN'
        ),
      ),
    );
  }
  return $query_args;
}

Replace 12345 with the numeric ID of your View, and replace day-of-the-week with the slug of your custom taxonomy. Nothing extra is needed in wp-admin.

#621910

Awesome. Thank you so much! Works great.