Skip Navigation

[Resolved] In a View Custom Field Filter I Need THIS_WEEK

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

Problem:
How to display this week posts comparing against custom date field

Solution:
To display this week post comparing the custom date field, you will require to use the view's filter hook "wpv_filter-query".

You can fine proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/in-a-view-custom-field-filter-i-need-this_week/#post-2067171

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

This support ticket is created 3 years, 7 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 16 replies, has 2 voices.

Last updated by Timothy 3 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#2064029

Hi,

I've found a few other tickets requesting this from a while back, and I see there still isn't the option to select THIS_WEEK in the Query Filter:

hidden link

Is there any workaround yet available for this? It would really be helpful to be able to filter my items by a custom date field that is set to dates of the current week.

Thanks,

Tim

#2064065

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Toolset Types stores the custom date field value as Unix Timestamp so there is no native way to apply the "This Week" filter.

Can you please consider how you would like to consider this_week or you mean to display last 7 days posts - having said that 7 days old posts?

#2064233

Hi. I'm basically building a way to track working hours for a freelancer, so as he enters his hours/fee day by day he can see how many hours he has worked, and money earned, this current week so far.

So last 7 days won't work for this type of requirement. You can see the progress I've made so far here:

hidden link

Tim

#2064237

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

So how you want to setup "this _week" I'm not sure. If you can tell me your requirement how you want to filter using the custom date field I can guide you in the right direction.

#2064243

In the table in you can see the Work Date column. So I want to filter by the dates that are within this current week.

#2064275

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What if you change your "Work Date" frontend filter you added to view's "Search and Pagination" section to use between so it will offer you the start work date and end work date as options and user will select the start work date and end work date and based on the selected dates user will be able to filter the results.

Does that work for you?

#2064297

Sorry, I think I didn't explain well. So in the grey boxes you can see calculations for money earned and hours worked. So I have 2 views for that. They add up the hours and money of each new form entry. Now they are looping through all entries to calculate the money earned and hours. I want them to just add this current week's hours and money.

#2064303

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What will be the range of "this_week" - is it Monday To Friday or Monday to Sunday?

#2064447

Monday-Sunday

#2064977

So I came across this support thread that looks like it has a potential solution:

https://toolset.com/forums/topic/query-filter-this_week-future_week-alternative/

I've created the query filter, registered the shortcode, and added the view shortcode with the start/end attributes:

[wpv-view name="work-hours-this-week-view" start="[ts-thursday index='0'][/ts-thursday]" end="[ts-thursday index='1'][/ts-thursday]"]

But it's not working and I think I'm just not getting the shortcode right, here's what I have now in my functions file:

add_shortcode( 'ts-thursday', 'ts_thursday_func');
function ts_thursday_func( $atts, $content ) {
  $atts = shortcode_atts( array(
    'index' => 0
  ), $atts );
  $week_index = $atts['index'];
  $timestamp =  strtotime("this Monday");
   	
  // your custom code here should calculate the correct value for $timestamp
  // $week_index will default to 0 unless the User provides one in the shortcode: $week_index
  
  return $timestamp;
}

I just took a guess at the timestamp part, I really don't know php much at all. Maybe this is where the issue is?

Tim

#2066099

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - that way you can filter the records. If you can share admin access details I will adjust the code and show it to you.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2066345

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've added the following filter to calculate the this week earned money under label "Earning This Week":

add_filter( 'wpv_filter_query', 'func_filter_this_week_posts', 999, 3 );
function func_filter_this_week_posts( $query_args, $view_settings, $views_id  ) {
    if ( $views_id == 2989  ) {
      
      if ( (isset($query_args['meta_query'])) && (!empty($query_args['meta_query'])) ) {
  
             $target_field = "wpcf-work-date"; 
       
            foreach ($query_args['meta_query'] as $index => $value):
        		if(isset($value['key']) and $value['key'] == $target_field) {
                  		$target_index = $index;
                        $days = explode(",",$value['value']); 
                        $start = strtotime("this $days[0]");
                  		$end = strtotime("this $days[1]")+86400;
                        unset($query_args['meta_query'][$target_index]);
           		}
            endforeach;
            
           if($target_index >= 0){
             $query_args['meta_query'][$target_index] = array('key' => $target_field, 
                                                                'value' => $start.",".$end, 
                                                                  'type' => 'UNSIGNED', 
                                                                  'compare' => 'BETWEEN' );
             } 
  
        
            
        }   
   }
    return $query_args;
}

This is how the view shortcode is added:

[wpv-view name="work-hours-this-week-view" start='Monday' end='Sunday']

Can you please check now and and review if that works as expected.

#2067171

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've adjusted the code as given under at the "Custom Code" section offered by Toolset:

add_filter( 'wpv_filter_query', 'func_filter_this_week_posts', 999, 3 );
function func_filter_this_week_posts( $query_args, $view_settings, $views_id  ) {
    if ( $views_id == 2989  ) {
      
      if ( (isset($query_args['meta_query'])) && (!empty($query_args['meta_query'])) ) {
  
             $target_field = "wpcf-work-date"; 
       
            foreach ($query_args['meta_query'] as $index => $value):
        		if(isset($value['key']) and $value['key'] == $target_field) {
                  		$target_index = $index;
                        $days = explode(",",$value['value']); 
                        $start = strtotime("$days[0] this week");
                        $end = strtotime("$days[1] this week")+86400 -1;
                        unset($query_args['meta_query'][$target_index]);
           		}
            endforeach;
            
           if($target_index >= 0){
             $query_args['meta_query'][$target_index] = array('key' => $target_field, 
                                                                'value' => $start.",".$end, 
                                                                  'type' => 'UNSIGNED', 
                                                                  'compare' => 'BETWEEN' );
             } 
  
        
            
        }   
   }
    return $query_args;
}

I can see the earning of this week is 100:
=> hidden link

#2067311

Excellent, yes this appears to work perfectly. Thanks so much. If I want to apply it to 2 or more views, would I just change this line?

if ( $views_id == 2989 ) {

#2067331

I adjusted that line to this, and now it can be used in multiple views:

if ( in_array($views_id, array(2989, 3012))) {