Skip Navigation

[Résolu] Filter archive view using cookies

This support ticket is created Il y a 6 années et 10 mois. 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
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 1 réponse, has 2 voix.

Last updated by Christian Cox Il y a 6 années et 10 mois.

Assisted by: Christian Cox.

Auteur
Publications
#539540
Post.png
Age Groups not filtering.png

I am trying to: filter 'views' and 'archive views' using cookie values set by user's preference.

The website has two drop downs on top left which allows user to search for 'city' and 'age-group' preferences. These values are saved in cookies and later used to filter the various views using wpv_fiter_query option.

'cities' and 'age-groups' are custom taxonomies.

Various 'views' work as expected.

But 'Archive' view (view id: 52819) does not seem to work. It does not honor age-group filtering.

Here is the code used:

add_filter( 'wpv_filter_query', 'ksp_events_home', 999, 3 );
function ksp_events_home( $query_args, $view_settings, $view_id ) {
	$_apply_filter_view = array("77962","52363","52317","65094","52084","52819","55779","53165","55707","58595");
   
    if (in_array($view_id,$_apply_filter_view)) {
      
        global $_curr_user_city,$_curr_user_age; //echo $_curr_user_city."<==>".$_curr_user_age;
        if(!empty($_curr_user_city) || !empty($_curr_user_age)){
            if(!empty($_curr_user_city)){
                $query_args['tax_query'][] = array(
                    'taxonomy' => 'city',
                    'field'    => 'term_id', //term_id
                    'terms'    => array($_curr_user_city),
                    'operator' => 'IN',
                    'include_children' => 1
                );
            }
            if(!empty($_curr_user_age)){
                $_curr_user_ageArr = explode(",",$_curr_user_age);

                $query_args['tax_query'][] = array(
                    'taxonomy' => 'age-group',
                    'field'    => 'term_id',
                    'terms'    => $_curr_user_ageArr,
                    'operator' => 'IN',
                    'include_children' => 1
                );
            }
            $query_args['tax_query']['relation'] = 'AND';
        }
    }
    return $query_args;
}

The programming team has written following function that seems to work

I visited this URL:

hidden link

Select age group : 4-6 years from the header top left drop down.

The post: 'Easy to make, healthy snacks for baby's daycare' should not show up.

Are we missing something in our code. Your help in the matter will be highly appreciated.

For your reference: Attached screenshot shows that the post Easy to make, healthy snacks for baby's daycare' does not belong to 4-6 years category. However, it still shows up.

Your help in the matter will be highly appreciated.

#539695
Screen Shot 2017-06-21 at 9.25.14 AM.png

Hi, the wpv_filter_query filter does not apply to WordPress Archives, only to Views. In order to apply a taxonomy filter to a WordPress Archive, you must specify the taxonomy term in the Query Filter settings of the WordPress Archive, or configure a Query Filter to respond to a taxonomy term provided as a URL parameter.

The best way I can think of to accomplish what you would like is to use the native WordPress pre_get_posts function to filter this archive:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

The only "Toolset way" I can think of is to use your PHP cookie to build links to these archive pages with URL parameters applied for each cookie value. Then set up your WordPress Archive to include a Query Filter that responds to URL parameters. For example, your food reviews category link might look like this for a user with cookies: hidden link. Then the filter would show the appropriate results. I'm attaching a screenshot here that shows how a similar Query Filter is set up in the WordPress Archive editor.

The only related filter we offer for WP Archives is the wpv_filter_force_wordpress_archive filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_wordpress_archive
However, I don't think this would be useful because it only allows you to specify a different archive ID to apply to the current archive page. You would have to set up separate archives for each possible combination of your two cookies...this is not practical when you have more than a handful of options.

Let me know if you have follow up questions about any of these options and I'll try to assist.

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