Skip Navigation

[Resolved] Initialize a view with posts that match todays date

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

Problem:
Initialize a view with posts that match todays date

Solution:
You can use view's filter "wpv_filter_query" to filter the posts by current date or today's date by default.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/initialize-a-view-with-posts-that-match-todays-date/#post-2296447

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

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 3 replies, has 2 voices.

Last updated by Simon 2 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#2293925
toolset_views_today.png

Initialize a view with posts that match todays date with the ability to change the search criteria (date picker) to another day in the future.

The view however shows all posts from today and forward when first visiting the page.
After selecting another date the view only shows the selected posts with the matching date which is correct.

How can I initalize a view that match only today() on the first visit and then be able to select another date in the future?

URL: cnema.se/startsida-test-privat-bishop/

#2294259

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

To display the posts based on current date by default, you will have to use the view's filter hook: wpv_filter_query.

When you say you want to display posts from today's date by default, do you mean that you want display posts having date grater than equal to current time and before the end of date hour 12:59:59?
- If yes, can you please share problem URL and admin access details.

*** 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.

#2296447

Minesh
Supporter

Languages: English (English )

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

As I do not have FTP access or even Toolset offers "Custom Code" section where we can add the code snippets but when I try to create code snippet it seems on server there is no rights:
- hidden link

I've installed and activated the "Code Snippet" plug9in and I've added the following view's filter code to the "Code Snippet" plugin's snippet:
=> hidden link

add_filter( 'wpv_filter_query', 'func_filter_custom_date_with_fulldaytime', 99, 3 );
function func_filter_custom_date_with_fulldaytime( $view_args, $settings, $view_id ) {
   
    if ( $view_id == 12888) {
		
		 if (!isset($_REQUEST['search']) ) {
			  
			   $target_field = "wpcf-session_showtypestimestamp"; // change this field slug to your field slug
  
            
            foreach ($view_args['meta_query'] as $key => $value):
                                       if ($value['key'] == $target_field){
                                                              $day_start_time =   $value['value'];
                                                              $day_end_time = strtotime('+1 day', $day_start_time ) - 1;
										    				  
                                                              $filer_index = $key;
										   					
										                      break;
                 						}
            endforeach;
             
  
            if (isset($filer_index)){
				
                  
                $view_args['meta_query'][$filer_index] = array('key' => $target_field, 
                                                                  'value' => array($day_start_time, $day_end_time), 
                                                                  'type' => 'NUMERIC', 
                                                                  'compare' => 'BETWEEN' );
             }       
        }
		
     }
    return $view_args;
}

Can you please confirm you can see today's posts by default:
- hidden link

#2296627

My issue is resolved now. Thank you very much!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.