Skip Navigation

[Resolved] Writing a conditional display based on user values

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

Last updated by Minesh 8 months, 1 week ago.

Assisted by: Minesh.

Author
Posts
#2777813

Hi,
I need to write a conditional display that will only show "jobs" that are related to certain user fields.

I have managed to add USER checkboxes for Location (e.g. Location 1 and Location 2) and a separate set of checkboxes for Sector (e.g. Education, non-profit, tech).

I have also added matching categories to the "jobs" custom post type (CPT). The categories match the values of the USER checkboxes (i.e. they have the same name.

On the conditional display, I need the "jobs" that have categories that match the checkbox values of the USER to be displayed.

Example 1:
User 1 is logged in and has Location 1 and Sector 1,3 & 6 checked on their profile. When viewing the "jobs" listing, only "jobs" that are categorised as Location 1 and/or Section 1,3 & 6 should be visible to the logged in user.

Example 2:
User 2 is logged in and has Location 2 checked on their profile. When viewing the "jobs" listing, only "jobs" that are categorised as Location 2 should be visible to the logged in user.

Example 3:
User 3 is logged in and has Sector 2 & 5 checked on their profile. When viewing the "jobs" listing, only "jobs" that have matching categories (Sector 2 & 5) should be visible to the logged in user.

#2777845

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share problem URL where you are showing job listing page as well as admin access details for frontend user where user profile is setup with location and sector.

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

#2777893

Minesh
Supporter

Languages: English (English )

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

Ok and what if user did not selected any option for location or section? we should not display any result or display all results?

#2777898

The fields will be required on the registration form, but if no option is selected, no results should be displayed and perhaps a message should be displayed instead.

"You'll need to select a location and or a sector in order to view these results."

#2777904

Minesh
Supporter

Languages: English (English )

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

The first thing is that, you should have identical slug so that we can match the category term slug with the custom user field value.

So, as you can see with your user custom field group:
- hidden link

I've set the value of your custom field as per the category slug:
- hidden link

For instance, the slug is "gauteng" for category "Gauteng", I have changed the location user field option value as "gauteng" for the option "Gauteng". So we can match the category slug equal to your custom field value. I hope that makes sense.

To you view, I've added the following query filter under the "Query Filter" section:
=> hidden link

Taxonomy filter
Select posts with taxonomy:
Categories slug in one of those set by the View shortcode attribute wpvcategory
eg. [wpv-view name="view-name" wpvcategory="xxxx"]

And configure the no items found message when there is no selection of location or section field in user profile page it will display the following message.:

[wpv-no-items-found]
<strong>[wpml-string context="wpv-views"]You'll need to select a location and or a sector in order to view these results.[/wpml-string]</strong>
[/wpv-no-items-found]

Then to your page:
- hidden link

I've added the "Fields and Text" block and added the following shortcodes that will display the current user selection for location and section fields:

User location: [types usermeta='location' output='raw' separator=', ' current_user='true'][/types] <br/>
User Sector: [types usermeta='sectors' output='raw' separator=', ' current_user='true'][/types]
<br/>

And then added the View's shortcode as given under:

[wpv-conditional if="( '[types usermeta='location' output='raw' current_user='true'][/types][types usermeta='sectors' output='raw' current_user='true'][/types]' ne '' )"]	
  		[wpv-view name="tender-list-3" wpvcategory="[types usermeta='location' output='raw' separator=',' current_user='true'][/types],[types usermeta='sectors' output='raw' separator=',' current_user='true'][/types]"]
  [/wpv-conditional]

[wpv-conditional if="( '[types usermeta='location' output='raw' current_user='true'][/types][types usermeta='sectors' output='raw' current_user='true'][/types]' eq '' )"]	
  		[wpv-view name="tender-list-3" wpvcategory="0"]
  [/wpv-conditional]

Where:
- We are checking for location and section field and if (location or section field) is not empty then it should get the value using the [types] shortcode and assign the value to shortcode attribute "wpvcategory":
- if (location or section field) is empty we pass the 0 to shortcode attribute wpvcategory="0"

For now, my user profile do not have any selection so it displays the message "You'll need to select a location and or a sector in order to view these results.":
- hidden link

Can you please check now: hidden link

More info:
- https://toolset.com/course-lesson/filtering-custom-lists-of-posts/#value-set-by-view-shortcode-attribute
- https://toolset.com/course-lesson/filtering-custom-lists-of-posts/#filtering-by-taxonomy
- https://toolset.com/documentation/legacy-features/views-plugin/conditional-html-output-in-views/

#2778118

Hi Minesh. Thank you so much for this! It's going to take me awhile to try and implement/understand it but I'll reply asap.

#2778159

Minesh
Supporter

Languages: English (English )

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

Ok sure, take your time and please get in touch with me if you require further assistance.