Skip Navigation

[Resolved] Filter by (remove-date earlier < today or remove-date is null) and company =

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

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 5 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1275943

Hello,

This is for a custom jobs board. hidden link

I want to create a filter in my view that uses a new custom field "remove-date" and another custom field "company."

The remove-date may or may not have a value in it, so valid results are "remove-date is less than today" or "remove-date has no value" and custom field company = a value. Basically, (this or that) and (this).

The issue is that I have lots of records that don't have a remove date (it's a new field) and may not even need one - but I do need to keep track of remove dates and use to filter results when they are entered.

I'm hoping I'm just missing something obvious.

- Peter

#1275995

Hi, you're not missing anything obvious unfortunately. There's just not an easy way to accomplish this in the wp-admin Views editor because complex and/or combinations aren't supported among custom fields. You can choose "AND" or "OR" once, without separate grouping. The type of filtering you described requires custom code. We offer the wpv_filter_query filter that allows you to modify the WP_Query parameters for any View. You can find out more about implementing this filter here: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

Here's the WP_Query reference for handling custom field parameters: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
There's an example there for handling multiple fields, in combination queries.

#1276039

Got it.

Is there documentation, help, or examples on how to apply a wpv_filter_query filter to a view?

Assuming I can get the syntax correct on the filter query itself, I'm missing how it is "assigned" to the view.

#1276041

Sure, you can use this template to "assign" a filter to a single View or multiple Views:

add_filter( 'wpv_filter_query', 'your_custom_filter_name', 10, 3 );
function your_custom_filter_name ( $query_args, $view_settings, $view_id ) {
  $views = array( 1234, 5678 );
  if( in_array( $view_id, $views) ) {
    // add your custom query manipulation code here
  }
  return $query_args;
}

Change 1234, 5678 to be a comma-separated list of View IDs. If you only want to apply the filter to one View, just use one View ID with no comma. You can find View IDs in the main View dashboard list in wp-admin > Toolset > Views.
Change your_custom_filter_name in both places to something that makes sense for your project. Use lowercase letters, numbers, and underscores, no spaces or special characters.

Here's an example showing how to completely override the custom field filters you create in the View editor:

add_filter('wpv_filter_query', 'add_custom_fields_to_view_query', 199, 3);
function add_custom_fields_to_view_query($query_args, $view_settings, $view_id ) {
  $view_ids = array( 1234 );
  if( in_array( $view_id, $view_ids)) {
    $query_args['meta_query'] = array(
        'relation' => 'AND',
        'year_clause' => array(
            'key' => 'wpcf-ce-year',
            'compare' => 'EXISTS',
            'type' => 'NUMERIC',
        ),
        'month_clause' => array(
            'key' => 'wpcf-current-entries-month',
            'compare' => 'EXISTS',
            'type' => 'NUMERIC',
        ),
    );
  }
  return $query_args;
}