Skip Navigation

[Resolved] How to use wpv_filter_query to filter posts in a loop

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
- 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 2 replies, has 2 voices.

Last updated by nicolaT-2 1 year, 3 months ago.

Assisted by: Minesh.

Author
Posts
#2619585

Hi, we need to select posts in a loop filtering them according to the "primary" element in the taxonomy "branca-tax" (primary status is set by Yoast SEO plugin).

More in detail: in the template "location" (for the CPT "locations") we have a view that prints the CPT "services" (filtering only the services of the current location), this view contains a nested view that shows elements from the CPT "doctors". Here, we need to filter these doctors according to their primary "service" taxonomy (it's slug is "branca-tax"): if a doctor has more than one service selected, we need to use only the primary one.

We tried to use the following code, which you gave us answering to this ticket (solved) https://toolset.com/forums/topic/show-only-primary-taxonomy-in-a-view/. Since we are triyng to make something a little different it must be adjusted:

--- START CODE ---

<?php
/**
* Filter only "Primary" branca-tax in locations
*/

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', 'func_filter_by_yoast_primary_term_in_locations', 99, 3);
function func_filter_by_yoast_primary_term_in_locations( $query_args, $view_settings, $view_id ) {
global $post;
if($view_id == 3083 ){

$query_args['tax_query'][0]['terms'] = 0;
$primary_term = get_post_meta($post->ID,'_yoast_wpseo_primary_branca-tax',true);
if(!empty($primary_term)) {
$query_args['tax_query'][0]['terms'] = $primary_term;
}

}
return $query_args;
}

--- END CODE ---

Now this code is giving zero results. I suppose it's because the "$primary_term = get_post_meta($post->ID..." is referring to the post where the view is used (the location), instead we need to look at the post in the loop but maybe there's something more...

Can you please help me?

Thanks,
Nicola

#2619595

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

It does not require I guess to add "wpv_filter_query" hook most probably if its available to add the taxonomy filter with "Query Filter" section of your view.

Can you please share problem URL where you added your view and what is your expected result.

*** 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.

#2620199

Hi Minesh, sorry for the late reply, my client has chosen to show only the Services and hide the Doctors, so we no longer need your help at the moment.

Thanks anyway,
Nicola

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