Skip Navigation

[Gelöst] Filter a View by custom field and current day of the week

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: I would like to use a custom field on my post to determine when it appears in a View. The field should have options for each day of the week, and the post should only appear on the selected day of the week.

Solution: Set the options for your custom field to have values that correspond to each day of the week, using lowercase letters, like "monday", "tuesday", and so on. Then add this code to functions.php:

add_filter( 'wpv_filter_query', 'todays_posts',99,3 );
function todays_posts( $query,$views_settings, $view_id) {
  $views = array( 1234, 5678 );
  if ( in_array( $view_id, $views ) ){
    $args = array(
      'relation' => 'AND',
      array(
        'key' => 'wpcf-yourfieldslug',
        'value' => date('l'),
        'compare' => '='
      )
    );
    $query['meta_query'] = isset($query['meta_query']) ? $query['meta_query'] : [];
    $query['meta_query'][] = $args;
  }
  return $query;
}

Replace 1234,5678 with a comma-separated list of View IDs where you want to apply this filter. Replace "yourfieldslug" with the slug of your custom field.

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

This support ticket is created vor 6 Jahren, 10 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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)

Dieses Thema enthält 13 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Christian Cox vor 6 Jahren, 9 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#612453

Hi,

I'm creating a View, and I'd like to filter by day of week. In the CPT, my custom taxonomy stores data as day of week in string format, e.g. Monday, Tuesday, etc. For the View, I'd like to be able to filter by day of week based on today's date. Can you advice how I can do this?

#612537

I don't know of a way to do this in the Views GUI, because taxonomy filters do not offer any date-based options. Instead, you could use custom code that automatically applies a filter to the query. Assuming the term slugs are "monday", "tuesday", and so forth, the following code in functions.php will filter 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.

#613128
Screen Shot 2018-02-06 at 1.39.36 PM.png

Hi Christian,

I have tried adding your code via both editor and Code Snippet, but not getting any filter result. Please see the image as attached.

Also, I understand this code filter by only the specified "day-of-week", is that right? What if I want the slug to change based on today's actual day? For example, for today it is to filter by Tuesday. For tomorrow, filter by Wednesday.

Thank you so much and looking forward to your reply.

#613450

I have tried adding your code via both editor and Code Snippet, but not getting any filter result.
Let me clarify - you won't notice anything different about the dropdown filter options in wp-admin. In fact there is no need to add a Query Filter (unless you want to use additional filters we have not discussed). The code we added to your PHP file will be invisible in wp-admin, and will be applied programmatically.

What if I want the slug to change based on today's actual day? For example, for today it is to filter by Tuesday. For tomorrow, filter by Wednesday.
This is exactly how it works now. In the code below, we are creating a dynamic term filter based on the current day of the week -
date('l'). So on Monday, the term slug filter is "monday". On Tuesday, the term slug filter is "tuesday", and so on. No changes are necessary for this to work like you have described.

'tax_query' => array(
        array(
          'taxonomy' => 'day-of-the-week',
          'field'   => 'slug',
          'terms' => strtolower(date('l')),
          'operator' => 'IN'
        ),
      ),
#613855
Screen Shot 2018-02-08 at 12.39.22 AM.png
Screen Shot 2018-02-08 at 12.38.22 AM.png
Screen Shot 2018-02-08 at 12.35.04 AM.png

HI Christian,

I put the code into Code Snippet for functions.php and I got an error No items found. Can you please advice?

#613879

Sure, let's try these troubleshooting steps:
- Edit this View and uncheck "Don't include the current page in results", that can cause unexpected problems.
- Go to Toolset > Taxonomies and edit your custom "Day of the Week" taxonomy. Make sure that the slug of this taxonomy is "day-of-the-week". If it is not, you must modify the custom PHP code to match your taxonomy slug.
- Go to Events > Day of the Week so you can see a list of all the terms in the Day of the Week taxonomy. Edit each slug and make sure the slugs are "monday", "tuesday", "wednesday", and so on.
- When this is configured correctly, you should be able to see an archive of Events tagged with each day term at these URLs:
versteckter Link
versteckter Link
versteckter Link
versteckter Link
versteckter Link
versteckter Link
versteckter Link
- If your taxonomy slug is not "day-of-the-week", the URLs above should be modified to match your taxonomy slug.

Let me know how it goes for you. If this isn't working as expected, I may need to log in to your wp-admin area and take a closer look. I will activate private reply fields for you here.

#613919

Hi Christian,

I think the problem is the taxonomy slug. I'm actually using a custom field, not custom taxonomy. The custom field called day of week is stored as a string data type, using a radio form field.

Can you advice what I should do in this case?

#613926

I'm confused. What is the custom taxonomy you mentioned here?

In the CPT, my custom taxonomy stores data as day of week

#614033

Sorry. It should be the custom field of a CPT. Please forget about the taxonomy. Can you help?

#614277

Okay the code above was designed for a taxonomy filter, so please disregard it. Here is the code optimized for a custom field filter:

add_filter( 'wpv_filter_query', 'todays_posts',99,3 );
function todays_posts( $query,$views_settings, $view_id) {
  $views = array( 1234, 5678 );
  if ( in_array( $view_id, $views ) ){
    $args = array(
      'relation' => 'AND',
      array(
        'key' => 'wpcf-yourfieldslug',
        'value' => date('l'),
        'compare' => '='
      )
    );
    $query['meta_query'] = isset($query['meta_query']) ? $query['meta_query'] : [];
    $query['meta_query'][] = $args;
  }
  return $query;
}

Replace 1234, 5678 with a comma-separated list of View IDs where you want to apply this filter, and replace 'wpcf-yourfieldslug' with 'wpcf-' + your day of the week field slug. This code assumes your custom field values are "Monday", "Tuesday", and so on.

#615097

Hi Christian,

Thanks for the code. I added it to Code Snippet, and still not working as I expected. I understand that is because I have additional filtering criteria in the View, and those were not used when the code is customized. What I need are

Filter by boolean-type field for true/false called weekly-event
Order by int-type field called Priority

#615324

I understand that is because I have additional filtering criteria in the View, and those were not used when the code is customized.
The custom code we applied does not affect the orderby attribute of a query, so any orderby attribute applied to the View elsewhere should be respected. The code also maintains any other meta queries that are already applied to the View, so the weekly-event field should still be applied.

At this point, I think it makes sense for me to create a clone of your site so I can investigate in more detail. I can install the Duplicator plugin and begin creating that clone if you agree. I will provide private reply fields here so you can share login credentials.

Please let me know which View I should be looking for, and where I can see that View on the front-end of the site.

#615372

You can access the View on the home page at versteckter Link

#615424

Please check the home page now. It looks like the featured todays event banner View wasn't placed on the page correctly, instead the featured-special-events-banner View was placed twice. Also the Content Template applied to the featured todays event banner View was incorrect, maybe this View was cloned from another View and the old Content Template was placed here? Not sure. I replaced that with a new Content Template, and I think things are looking better now. Salsa Mambo Monday is now appearing in the results, which appears to be accurate.