Skip Navigation

[Resolved] Change Custom field filter on different pages

This support ticket is created 3 years, 10 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 7 replies, has 2 voices.

Last updated by ronM-3 3 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#2337069

Hi,

Site in Development: hidden link

I created a view 'Werkvormen' on CPT Werkvormen.
I'd like to use this view at at least 2 different pages showing different results.
A custom field 'Pagina-werkvorm' defines whether a werkvorm should show on Page1, Page 2 or on both pages.

The view should show team-related werkvormen on page hidden link. (Pagina-werkvorm string in ('Team', 'Both')

Page hidden link should show werkvormen that are person-based. (Pagina-werkvorm string in ('Individual', 'Both')

However, changing the filter on one page also influences the results on the other page.

Question: Is it possible to somehow set the filter on a per-page basis?

#2337163

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ron,

Thank you for getting in touch.

Based on what I see here you basically want the same view on 2 pages but with filters for 2 different post types correct?

If so then I would recommend creating a copy of your view and changing the setting on the copy to reflect what you want on the second page.

Are you aware of how to create a duplicate for your view ? Are you creating your view with the classic editor or are you using the Gutenburg editor to create your view.

Please let me know.
Thanks,
Shane

#2337271

Hi Shane,

Thnx for your reply.

No, I want the same view with the same custom post type. The filter needs to be different on the 2 pages because I want to show other records.

I am using the Gutenberg editor.
I know how to duplicate a view. I could do that but I thought that that wouldn’t be necessary.
Regards,
Ron

#2338993

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ron,

In order to understand correctly, you're using the same filter on the frontend correct but you essentially want the results to be prefiltered differently based on the page correct?

Please let me know.
Thanks,
Shane

#2339087

Hi Shane,

I created a CPT 'Werkvormen' and added a fieldgroup with custom fields.
One of these custom fields is called 'page-to-display-on'.
This custom field contains a value of either 'Page A', 'Page B' or 'Both'.

Now on Page A I want to show all 'Werkvormen' that meet the criteria "'page-to-display-on' IN ('Page A', 'Both')".
On Page B I want to show all 'Werkvormen' that meet the criteria "'page-to-display-on' IN ('Page B', 'Both')".

I use the same view on both pages.
Is it possible to set/change the filter on Page A and Page B so that the correct 'Werkvormen' are shown on these pages?

Or could you advise on another solution?

Regards,
Ron

#2339183

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ron,

Essentially you want the view to be prefiltered based on the page that the view is on.

This case then we should be able to achieve this with one of our view hooks. Would you mind allowing me to have admin access to the website as well as a link to the page where I can see the views.

Thanks,
Shane

#2339747

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ron,

Thank you for the credentials.

This should now be working. Your view should now be filtering based on the page that it is on.

I've used the hook below to perform this filter for you.

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_filter( 'wpv_filter_query', 'filter_basedon_page', 10, 3 );
 
function filter_basedon_page( $query, $settings, $view_id ) {
   $postid = get_the_ID();
    if ( $view_id == 5635 && $postid == 30  ) {
       $query['meta_query']= array(
								array(
									'key'     => 'wpcf-pagina-werkvorm',
									'value'   => array('Team','Beide'),
                                  	'type'	  => "CHAR",
									'compare' => 'IN',
								),
					
				);
    }
   if ( $view_id == 5635 && $postid == 3876  ) {
       $query['meta_query']= array(
								array(
									'key'     => 'wpcf-pagina-werkvorm',
									'value'   => array('Beide','Individueel'),
                                  	'type'	  => "CHAR",
									'compare' => 'IN',
								),
					
				);
    }
    return $query;
}

Please let me know if this helps.
Thanks,
Shane

#2340785

My issue is resolved now. Thank you!