Skip Navigation

[Resolved] view query filter

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.

Our next available supporter will start replying to tickets in about 3.04 hours from now. 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 Minesh 9 months ago.

Assisted by: Minesh.

Author
Posts
#2683486
Screen Shot 2024-02-14 at 5.12.03 PM.png
Screen Shot 2024-02-14 at 5.08.45 PM.png

I'm trying to query a view with 2 post types (standard posts and a custom one called "events")

I want all the posts to show and ONLY the events with dates in the future. I can't seem to figure out how to set a query for that?
Screenshot of the dashboard I'm using. I would like the query to be something like:

the field start date and time is a number greater than or equal to today OR field start date is empty (posts don't have a start date).
however, once I choose the start date, it won't let me choose it again, so I tried adding another field that is required if it's an event (location) but it's forcing and AND between the two queries and I think it would work if it was an OR.

How can I accomplish this?
I tried using the shortcode in the loop editor, but it still reserves a spot for the event it just shows an empty post.

#2683498
Screen Shot 2024-02-14 at 5.22.16 PM.png

another screenshot

#2683562

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you confirm you want to display posts belongs to both post types "post" and "Events" both have the custom "State date and time" field is assigned?

And you want to also fetch posts where start date is future date or start date is not set - is that correct? if yes:

In that case we might have to use the view's filter hook "wpv_filter_query" but I will have to look at your current structure. Can you please share problem URL where you are trying to display the view as well as 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.

#2683779

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've removed all the "Query Filter" you added from the "Query Filter" section of your view:
- hidden link

I've added the following view's filter hook "wpv_filter_query" to the "Custom Code" section offered by Toolset:
=> hidden link

add_filter( 'wpv_filter_query', 'func_filter_by_start_date_exists',99,3);
function func_filter_by_start_date_exists($query_args, $view_settings, $view_id) {
    
    if($view_id==1587) {
      
      $all_event_ids = $all_post_ids = array();
      
      $post_args = array(
        'post_type' => 'post',
        'fields'    => 'ids',
        'num_of_posts' => -1,);
     
    $all_post_ids = get_posts($post_args);
      
      $event_args = array(
        'post_type' => 'gbs_event_event',
        'meta_query' => array(
            array(
                'key' => 'wpcf-gbs_event_start-date',
              	'compare' => '>',
              	'type'=>'numeric',
                'value'    => time(),
                ),
            ),
        'fields'    => 'ids',
        'num_of_posts' => -1,
    );
     
    $all_event_ids = get_posts($event_args);
      
      $query_args['post__in'] = array_merge($all_event_ids,$all_post_ids);
      
    }
     
      
  return $query_args;
}

More info:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

Can you please confirm it works as expected now.

#2683847

Yes, that seems to be working. THANK YOU VERY MUCH!