Skip Navigation

[Resolved] Filter output by number of week

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

Problem:

I am trying to filter meal plans by the current week and future weeks using the query filter of the views on my website.

However, the "THIS_WEEK" and "FUTURE_WEEK" options are not available, and I cannot format TODAY() or NOW().

Solution:

Use the Views API and the 'wpv_filter_query' hook to modify the query arguments and add a filter to show posts from the current week.

Edit the date field slug and View ID(s) in the provided code snippet, then add the custom code to your website.

// Function to filter meal plans by the current week
function tssupp_filter_view($view_args, $view_settings, $view_id)
{
    // Edit the date field slug to match your date field
    $date_field_slug = 'datum';
 
    // Edit the array with the View ID(s) you want to apply the filter to
    if (in_array($view_id, array( 123 ))) {
 
        // Get the start and end of the current week
        $current_week_start = strtotime("monday this week");
        $current_week_end = strtotime("sunday 23:59:59");
 
        // Check if meta_query is set and initialize it if not
        if ( !isset($view_args['meta_query']) ) {
            $view_args['meta_query'] = array();
        }

        // Add the filter to the query to show posts between the start and end of the current week
        $view_args['meta_query'][] = array(
            'key'      => 'wpcf-'.$date_field_slug,
            'value'    => array($current_week_start, $current_week_end),
            'compare'  => 'BETWEEN'
        );
    }
 
    // Return the modified view arguments
    return $view_args;
}

// Add the filter to the Views API
add_filter('wpv_filter_query', 'tssupp_filter_view', 101, 3);

Relevant Documentation:

Filtering Toolset Views by Dates:

https://toolset.com/course-lesson/filtering-toolset-views-by-dates/

Views Filters:

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

This support ticket is created 2 years, 5 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.

This topic contains 8 replies, has 3 voices.

Last updated by Michael Deutz 2 years, 5 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2549583
Screenshot 2023-02-07 165446.png

Tell us what you are trying to do?
I create a meal plan for several weeks. For each week I create a separate view in which the dishes of that week are displayed.

The CPT contains a date field. The date entered here must be compared with the current number of the week and all dates contained in the week must be output. How can I implement this with the query filter of the views?

Is there any documentation that you are following?
No

Is there a similar example that we can see?
No

What is the link to your site?
hidden link

#2550091

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

Would you please read the documentation below?

https://toolset.com/course-lesson/filtering-toolset-views-by-dates/

That will give you an idea on how to filter within a week.

Thanks.

#2550195
toolset.png

Hi Christopher,
unfortunately I have no way to format any of the given parameters. The document also does not contain any information on how I can format TODAY() or NOW().

If I had the ability to filter TODAY() within a conditional tag, I would have achieved my goal. If I specify the value '06' for the current week instead of TODAY(), the query works within the view template.
[wpv-conditional if="( '[types field='datum' format='W'][/types]' eq TODAY() )"]DO SOMETHING[/wpv-conditional]

The most easiest way would be to filter by "THIS_WEEK" and "FUTURE_WEEK", but these options don't exist.

Regards
Hendrik

#2550225
#2550921

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I checked the links and yes the THIS_WEEK or FUTUR_WEEK option is not available.

That needs to be custom coded.

I think it will need to be used within this hook:

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

But let me ask for a second opinion and see if there is something we can help you with.

I understand that you mentioned it is a feature request, but you adding it here will bump up the queue:

https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

Thanks.

#2550941

Christopher Amirian
Supporter

Languages: English (English )

Asked for a second opinion and will get back to you as soon as I have an answer.

#2551059

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi there

Christopher asked me to find a solution for you. In this case you will need to use the Views API, specifically the wpv_filter_query hook, to modify the query arguments to add a filter to only show posts from the current week. I have coded that on the basis of Monday through Sunday.

You will need to edit the slug of the date field (I have used "datum"; check that is the same as your date field) as well as the ID of the View you want to apply this to ("123" in the sample code below; you can provide multiple IDs in the array if you want to use this with several Views).

function tssupp_filter_view($view_args, $view_settings, $view_id)
{
    $date_field_slug = 'datum';

    if (in_array($view_id, array( 123 ))) { // Edit for View ID(s)

        $current_week_start = strtotime("monday this week");
        $current_week_end = strtotime("sunday 23:59:59");

        if ( !isset($view_args['meta_query']) ) {
            $view_args['meta_query'] = array();
        }
        $view_args['meta_query'][] = array(
            'key'      => 'wpcf-'.$date_field_slug,
            'value'    => array($current_week_start, $current_week_end),
            'compare'       => 'BETWEEN'
        );
    }

    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_view', 101, 3);
#2551125

Hello Nigel,
this works absolutely perfect for me. I was also able to make an adjustment for the coming weeks without any problems.
hidden link (pls see "Mittagstisch").

Thank you both so much for your great support!

Best regards
Hendrik

#2551127

My issue is resolved now. Thank you!