Skip Navigation

[Resolved] Filter by post relation

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.

This topic contains 18 replies, has 3 voices.

Last updated by thomasS-11 6 years, 10 months ago.

Assigned support staff: Luo Yang.

Author
Posts
#273268

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

211.png

2) You will need setup your view as below screenshot: 211.png
a) in section "Filter HTML/CSS/JS", add a tax filter:

[wpml-string context="wpv-views"]Product categories:[/wpml-string] 
	[wpv-control taxonomy="product-category" url_param="wpv-product-category" type="select"]

b) in section "Parametric search settings"
disable all AJAX feature,

c) in section "Layout HTML/CSS/JS", use current post link as the product post link:
[wpv-post-link ]

d) in your theme files, add below PHP codes:

function Filter_by_post_relation_func( $query_args, $view_settings, $view_id ) {
    if ($view_id == 123) { 
		$parent_slug = 'product';
		$tax_slug = 'product-category';
		$url_para = 'wpv-product-category';
		$child_posts = get_posts($query_args);
		$parent_ids = array();
		foreach($child_posts as $k => $v){
			$parent_id = get_post_meta($v->ID, '_wpcf_belongs_' . $parent_slug .'_id', true);
			$parent_ids[] = $parent_id;
		}
		if(isset($_GET[$url_para]) && $_GET[$url_para] != '0'){
			$query_args['tax_query'] = array(
				array(
					'taxonomy' => $tax_slug,
					'field' => 'slug',
					'terms' => $_REQUEST[$url_para],
					'operator' => 'IN',
				)
			);
		}
		$query_args['post__in'] = $parent_ids;
		$query_args['post_type'] = array($parent_slug);
	}
    return $query_args;
}

add_filter( 'wpv_filter_query', 'Filter_by_post_relation_func', 99, 3 );

Please replace 123 with your views post ID.

3) It is out the range of toolset, I suggest you try above.

#274680

Thank you very much Luoy, and sorry for the hassle..

It actually works now with the categories, but it wont display the applications in the selectbox from the filtering... im pretty sure i did it correct, as i followed the screenshot step by step..

When i remove the PHP snippet from functions.php, the Applications dropdown/selectbox works again...

I see you are using a older Views version, could that be the issue? Im using the latest update - i think so..

#274963

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

I am using the latest version of Views 1.6.4, above codes works fine in my localhost, since it is a custom PHP codes problem, if you need more assistance for it, please duplicate same problem in a test site, and fill below private detail box with login details, also point out the problem View and problem page URL, and where I can edit the PHP codes, I need a live website to test and debug.

#275003

So i found out that I was running Views beta (i forgot i was testing with beta version).
I did a manual complete uninstall and reinstalled stable version and it works like a charm.. thank you very much and once again, im sorry for the hassle..

By the way, there is a problem if downgrading to stable after beta, therefore i had to do everything manually (removing all traces from beta) otherwise you can't delete views, and your solution didnt work after downgrading by simply removing and reinstalling stable version.. therefore i did it manually, and your guide works..

Once again THANK you, i have left best feedback ever for you.