Skip Navigation

[Resolved] Filter View via CPT Taxonomy of logged in User

This thread is resolved. Here is a description of the problem and solution.

Problem:
Would like to filter a View of "Questions" where only posts are shown that match the role "expert" and where the category of the "Question" matches the category/categories of a users "Experts" post. There will only be one expert posts per user.

Example: Expert Post A > Author: User1 > Cat1,Cat4,Cat9

So the view is User1 one is logged in would show only questions of Cat1,Cat4,Cat9

Assign categories directly to the user but that relationship works as follows each user get has an Experts post where he is the author and has categories assigned. Theses categories should be matched against other CPT posts categories to filter.

Solution:
The solution is here with screenshots:
https://toolset.com/forums/topic/filter-view-via-cpt-taxonomy-of-logged-in-user/#post-562974

This support ticket is created 7 years, 3 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
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by michaelA-13 7 years, 3 months ago.

Assisted by: Noman.

Author
Posts
#562733

Tell us what you are trying to do?

Hi, I got a CPT "Experts" which has Categories matching another CPT "Questions"

I would like to filter a View of "Questions" where only posts are shown that match the role "expert" and where the category of the "Question" matches the category/categories of a users "Experts" post. There will only be one expert posts per user.

E.g. Expert Post A > Author: User1 > Cat1,Cat4,Cat9

So the view is User1 one is logged in would show only questions of Cat1,Cat4,Cat9

#562740

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Michael,

Thank you for contacting Toolset support. Your requirements are bit confusing, have you created Experts as CPT as well as Expert as User role? How the categories of Question CPT is connected to User role Expert?

1. Can you please provide step by step details that how you have created the View? (preferably with back-end screenshots)
2. What you expected to see on the front page?
3. What you actually see? (preferably with front-end screenshot and Link)

Thank you

#562791

Hi Noman, I actually would like to assign categories directly to the user but that relationship to the best of my knowledge is not possible. So I created the experts CPT. The relationship works as follows each user get has an Experts post where he is the author and has categories assigned. Theses categories should be matched against other CPT posts categories to filter.

I tried to illustrate as best as possible in following screenshot. I Hope this is now understandable.

hidden link

Michael

#562889

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for providing more information. I am going through the details and proceeding further on this. I will update you soon with the outcome of this.

Thank you for waiting

#562974

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

View 1.png
View 2.png
Result Page.png

I have achieved this by creating 2 Views and a custom shortcode [format_terms]. Below are the steps:

1. Create View1 for Export CPT >> and add Post author filter to it, screenshot attached.

2. Create View2 for Questions CPT >> and add Taxonomy filter to it, screenshot attached.

3. Please add this code in your theme’s or child theme’s functions.php file:

add_shortcode('format_terms', 'format_terms_fun');
function format_terms_fun( $atts ) {
	 // Attributes
    extract(shortcode_atts(
        array(
            'export_view_slug' => '', 
			'question_view_slug' => ''
        ),
        $atts
    ));
	
	$result = '';
	if(isset($export_view_slug) && $export_view_slug != '' && isset($question_view_slug) && $question_view_slug != '') {
		$result = '[wpv-view name="'.$export_view_slug.'"]';
		$result = do_shortcode($result);
		
		$result = trim(strip_tags($result));
		$result = preg_replace('!\s+!', ' ', $result);
		$result = str_replace(' ',',',$result);
		$result = '[wpv-view name="'.$question_view_slug.'" wpvbookauthors="'.$result.'"]'; //wpvbookauthors = shortcode attribute.
		$result = do_shortcode($result);
	}
	
	return $result;
}

4. Then add this custom shortcode & View in your Content Template or Page or where needed like this and replace your View1 and View2 slugs in this:

// export = student, questions = books

[format_terms export_view_slug='student-terms-of-logged-in-user' question_view_slug='get-books-by-terms-in-view-attribute']

5. Result page and output, screenshot attached.
==> In my example Export CPT = students | and Questions CPT = books

I hope it helps, you may have to modify something in it based on your setup.
Thank you

#563359

Thank you very much. Works like a charm!