Skip Navigation

[Résolu] Custom Field Query Filter

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: How to filter view by custom field of third party plugin/theme which stores serialize data.

Solution: You can use 'wpv_filter_query" filter to filter your view with custom fields with serialize data.

When displaying a View listing posts, this filter is applied to the arguments being generated by the View settings before they are passed to the WP_Query class. That means you can add/remove query on fly.

You can find the proposed solution with the following reply: https://toolset.com/forums/topic/custom-field-query-filter/#post-404625

Relevant Documentation:

  • Read the wpv_filter_query documentation for more information
  • Learn more about WP_query and how to use it in WordPress.

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 0.61 hours from now. Thank you for your understanding.

Marqué : 

This topic contains 6 réponses, has 2 voix.

Last updated by bambiM Il y a 5 années et 4 mois.

Assigned support staff: Minesh.

Auteur
Publications
#404560
query-filter.png
postmeta-table.jpg

Hi,

I'm trying to add "Query Filter" for a custom field set by the theme that I purchased. The problem is, I don't know how to filter custom field having multiple values (see database under postmeta table and views query filter, screenshot attached).

Custom Field (meta_key):
event_details

Values (meta_value):
• event_date_from: 07/01/2016
• event_date_to: 07/01/2016

I need to filter Upcoming Events and Past Events using event_date_from and event_date_to.

Need your help guys! Thanks in advance.

#404609

Minesh
Supporter

Languages: Anglais (English )

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

Hello. Thank you for contacting the Toolset support.

You can not filter view with such field which have multiple values using views filter. For such field I suggest you need to use view's filter hook: 'wpv_filter_query".

When displaying a View listing posts, this filter is applied to the arguments being generated by the View settings before they are passed to the WP_Query class. That means you can add/remove query on fly.

For example:

add_filter( 'wpv_filter_query', 'limit_time_on_query', 99, 3 );
function limit_time_on_query($query_args, $view_setting, $view_id) 
{
 if ($view_id == 999) {
    /// ADJUST your query_arguments with your meta query here as per your custom field as meta key and meta values here
 }
    return $query_args;
}

Where:
Please change "999" with your original view id.

More info:
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/

I hope above solution will help you to resolve your issue.

#404623

Hi Minesh,

Thank you for your quick response. I'm not a typical back-end developer. Can you give an example of meta_query inside of your sample statement?

#404625

Minesh
Supporter

Languages: Anglais (English )

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

Please check following code that may help you:

add_filter( 'wpv_filter_query', 'custom_search_criteria',10,2 );
function custom_search_criteria( $query_args ,$view_settings ) {
 if (isset($view_settings['view_id']) && $view_settings['view_id'] == 14840 ) {
    
             $index = 0;
             if(isset($query_args['meta_query'])){
                $index = count($query_args['meta_query'])+1; 
             }
             $query_args['meta_query'][$index] =   array(
                        'relation' => 'AND', 
                           array(
                                'key' => 'cf-college-cost-instate',
                                'value' => '',
                                'compare' => '!=',
                        ),
                        array(
                                'key' => 'cf-college-employed',
                                'value' => '',
                                'compare' => '!=',
                        ),
                        array(
                                'key' => 'cf-college-finaid',
                                'value' => '',
                                'compare' => '!=',
                        )
        );
     
}
return $query_args;
}

This will requie some PHP and Database knowldege to build such solution that is called custom programming.

More example:
https://toolset.com/forums/topic/parametric-search-by-year-range-2-numeric-fields/#post-298046

#404631

This will help me a lot. Thanks! I will let you know once I solve it.

Thanks for your help.

#404634

Minesh
Supporter

Languages: Anglais (English )

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

ok fine - thank you!!

#404955

Marked as resolved.