Skip Navigation

[Resolved] Setting View to current month on initial page load

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

Problem:
I am looking for a way to have posts shown with number equal to THIS_MONTH() on intial page load by using Views filter hook "wpv_filter_query"

Solution:
You can try filter hook wpv_filter_query like this:

add_filter('wpv_filter_query', 'remove_past_events', 10, 3);
function remove_past_events($query_args, $view_settings, $view_id) {
     
    if($view_id == 69798   && !isset( $_GET['wpv_view_count'] ))  {
        $time = time(); 
        $this_month = strtotime(date('Y-m-01 00:00:00'));
        $query_args['meta_query'][] = array(array(
            'key' => 'wpcf-program-tidspunkt',
            'value' => array($this_month, $time),
            'compare' => 'BETWEEN',
            'type' => 'NUMERIC,'
         ));
    }
     
return $query_args;
}

Relevant Documentation:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

This support ticket is created 7 years, 2 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by bobA-2 7 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#568154

Some time ago Minesh provided me support and the code below to remove past events from a view on initial page load. I was wondering if this can be modified to have the view show posts from the current month instead on initial page load?

So basically I am looking for a way to have posts shown with number equal to THIS_MONTH() on intial page load but still the user has to be able to choose other date range in a between filter with date pickers. I have no problem in setting up the latter but the initial load current month thing is the issue.

add_filter('wpv_filter_query', 'remove_past_events', 10, 3);
function remove_past_events($query_args, $view_settings, $view_id) {
   
if($view_id == 69798   && !isset( $_GET['wpv_view_count'] ))  {
$time = time();  
$query_args['meta_query'][] = array(array(
'key' => 'wpcf-program-tidspunkt',
'value' => $time,
'compare' => '>=',
'type' => 'NUMERIC,'
 ));
}
   
return $query_args;
}
#568349

Dear Bob,

Please try this:


add_filter('wpv_filter_query', 'remove_past_events', 10, 3);
function remove_past_events($query_args, $view_settings, $view_id) {
    
	if($view_id == 69798   && !isset( $_GET['wpv_view_count'] ))  {
		$time = time(); 
		$this_month = strtotime(date('Y-m-01 00:00:00'));
		$query_args['meta_query'][] = array(array(
			'key' => 'wpcf-program-tidspunkt',
			'value' => array($this_month, $time),
			'compare' => 'BETWEEN',
			'type' => 'NUMERIC,'
		 ));
	}
    
return $query_args;
}

More help:
https://stackoverflow.com/questions/11236708/how-to-get-timestamp-of-current-month-first-day-of-month-zero-hour
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

#568351

Thanks Luo. Works just fine.

#568373

You are welcome

#568847

Hi again.

It seems the code you provided only shows current month up until now and not the complete month?

#568864

Found the solution:
Changed time to mktime(23, 59, 59, date("n"), date("t"));