Skip Navigation

[Resolved] Displaying information relative to a logged in user

This support ticket is created 6 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1124583

Tell us what you are trying to do?
I have Custom Post Types for the following:
School
Group Member

And a Taxonomy:
Group

Each School CPT is associated with multiple Group Member CPTs
Each Group Member CPT is associated with only one School CPT
Each Group Member CPT can be tagged with multiple Group Taxonomies

Each Group Member entry contains a field called School Name

I have built a form where when someone enters a School name it will display the following information relative to that school:

School
Group 1
Group Member A
Group Member B
Group 2
Group Member C
Group Member D

I would like to make it so that instead of having to enter a school name the form will pull the "School Name" field from the logged in user and display the required information.

Here is the current search form view:
[wpv-filter-start hide="false"]

[wpv-filter-controls]
<div class="form-group">
<label>[wpml-string context="wpv-views"]Name of School[/wpml-string]</label>
[wpv-control-postmeta field="wpcf-name" url_param="wpv-wpcf-name" placeholder="School Name"]

[wpv-filter-submit output="bootstrap"]
</div>
[/wpv-filter-controls]
[wpv-filter-end]

It generates a page with just a search box, user enters a school name, the results page displays the required information relative to that school. I would like to make it so the user does not have to enter a school name in the search field. Instead when a logged in user visits the page it will just pull in the results based on the logged in user's custom field school name.

#1124766

Hi, generally speaking you can pass a value into a View's Query Filter using shortcode arguments. This is a great way to access information about the current User and pass it into the View's Query Filters. We have documentation about this available here: https://toolset.com/documentation/user-guides/passing-arguments-to-views

With that being said, this can be a bit tricky because it looks like your custom field value holds the school name. Views does not provide a built-in post title filter, only a text search field. I might be able to offer some better advice if you can answer these questions:
1. How are Users and Group Members associated with one another? Is each User the author of only one Group Member post?
2. You wrote:
"Each Group Member entry contains a field called School Name"
but also:
"it will just pull in the results based on the logged in user's custom field school name."
So is the custom field on the User's profile, or on the Group Member post, or both?
3. Are you using Toolset Relationships to link these CPTs? If so, why is there a School Name field? If not, it seems it would make more sense to connect posts using post IDs (which are static over time) instead of titles (which are variable over time), unless there is a good reason.

#1125293

Thank you but I think that is just confusing me more, no offense. Here is what I currently have.

On the User Profile I have added a custom field called "access-to-school" a single line text field. It contains the name of a school.

Here is my school info view:
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<ul class="wpv-loop js-wpv-loop">
<wpv-loop>

  • [wpv-post-title]
    [wpv-view name="groups"]
  • </wpv-loop>

    <!-- wpv-loop-end -->
    [/wpv-items-found]
    [wpv-no-items-found]
    [wpml-string context="wpv-views"]No items found[/wpml-string]
    [/wpv-no-items-found]
    [wpv-layout-end]

    Right now the school view currently displays all the correct information but it shows all schools, I need it to only show the school where the CPT Post Title is equal to the value in the User custom field "Access-to-school".

    #1125527

    Sorry for complicating things. Here is the simplest answer:
    I need it to only show the school where the CPT Post Title is equal to the value in the User custom field "Access-to-school"
    You need to filter a View by post title. I understand, but Toolset Views does not offer this feature built-in. If absolutely necessary, it is possible with custom code using our API wpv_filter_query. However there are important reasons to not use a post title filter.

    Let's say you have two different schools with the same name, like George Washington Elementary. The post titles are the same, so it's not possible to distinguish which school the User is related to.

    Let's say the School post title is changed in wp-admin because there was a spelling error in the original title. Once you change the post title, the Users who were previously related to that School are no longer related to the School, because the Users' custom field values no longer match the School post title. To resolve that, someone would have to edit the profile of each User associated with that School, and change the custom field value. That is not practical or manageable.

    Instead, it's better practice to compare the custom field value in the User's profile against the School post's numeric ID, which will not change over time.

    In short, don't use a post title in the User profile custom field value. Use a select field instead of a text field to connect with a School. The label of the field should be the School post title, and the content should be the School post ID.