Skip Navigation

[Resolved] Post relationship with CPT and user

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

Problem: How to fill user custom dropdown select field options dynamically.

Solution: You can use Types hook 'wpt_field_options' to fill out the options for your user custom field.

You can find proposed solution with the following reply: => https://toolset.com/forums/topic/post-relationship-with-cpt-and-user/#post-576811

Relevant Documentation:

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

Last updated by marcB-6 2 years, 10 months ago.

Assigned support staff: Minesh.

Author
Posts
#576486

I have CPT called "members(company)".
I would like to relate "members" to "delegates(users with role delegates)".
Users are getting registered from front end,so once user registers it will be listed inside "users" in wp admin and there I am having "Working at" dropdown on the wp-admin side.

"Working at" would be dropdown & it will have "members(company)" as options in dropdown.from there I can assign "company" to particular user.Now I have company detail page on front end in which I would like to display list of "delegates(users with role delegates)" working at that particular company.

To achieve above goal I tried to set parent/child post relationship but, I can't see "user" option inside CPT called "members(company)". Please check attached screenshot for better understanding.

How to achieve above goal.

#576798

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - you can not connect user with post type and build posttype relationship between post and users.

However, as I understand you want to list CPT Member post titles with "Working at" dropdown at user? Could you please be more specific what exactly you want to display with "Working at" dropdown as optons.

#576809

Hi Minesh,

You want to list CPT Member post titles with "Working at" dropdown at user

Answer : Yes

Thanks

#576811

Minesh
Supporter

Languages: English (English )

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

Ok - I think you should use Types hook 'wpt_field_options' to fill out the options for your user custom field.

For example:

//dynamically populate select field from Types
add_filter( 'wpt_field_options', 'prefix_custom_options', 10, 3);
 function prefix_custom_options( $options, $title, $type ){
    switch( $title ){
        case 'working at member':
            $options = array();
            $args = array(
                'post_type'        => 'post',
                'post_status'      => 'publish');
            $posts_array = get_posts( $args );
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                );
            }
            break;
    }
    return $options;
}

Where:
- Adjust your post_type slug with above code with your user custom field title.

So, now you can save the member post ID on user profile.

#576852

Hi Minesh,

Above code is working and I can see "members" in "working at" dropdown. As I have mentioned in my first post "from there I can assign "company" to particular user.Now I have company detail page on front end in which I would like to display list of "delegates(users with role delegates)" working at that particular company."Please see screenshot as well from first post and current post.

I have done following in content template.

[wpv-view name="member-delegates" id="[wpv-post-id]"]

but I can not see list of users working at "members(company)."

Thanks

#576865

Minesh
Supporter

Languages: English (English )

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

As I understand - now you want to display on single company page all the members who is working on that company - correct? If yes:

Could you please share problem URL with test content setup (member, user).

*** 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 would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#577183

Hi Minesh,

As I see you are working currently.. Correct? How much time you need to work on this? So, Once you finish your work we can continue our work.

Thanks

#577186

Minesh
Supporter

Languages: English (English )

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

Well - the page you shared with me, when I try to load that page it ask me for login eventhough I used to be login.

However - I've created test page and its showing correct results. Please check the changes I made with view's filter.
=> hidden link

Also - I deactivated "Buro Tijs theme mods" plugin for now. You can activate if once you check above page.

#577188

Hi Minesh,

It is working but I see below lines in one of my page.
Content Template debug:
Calling functions are call_user_func
Above errors are gone as I activated "Buro Tijs theme mods" plugin.
Did you change theme before when you were working?

I have other dropdowns as well in users that is "committee" dropdown, In that I would like to display other CPT.
The filter you gave above If I add new cases. Will it work for me?

Please check attached screenshot.

Thanks

#577192

Minesh
Supporter

Languages: English (English )

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

It is working but I see below lines in one of my page.
Content Template debug:
Calling functions are call_user_func
Did you change theme before when you were working?
==> OK - I've fix that and deactivate the setting 🙂

I have other dropdowns as well in users that is "committee" dropdown, In that I would like to display other CPT.
The filter you gave above If I add new cases. Will it work for me?
==> Yes - you need to adjust your as per your need and filed name.

#577194

Ok Thanks Minesh 🙂

#577228

Hi Minesh,

In "Working at member" dropdown I see all "Members" that is fine..How to add "select" option as first option in that dropdown just to show default value in dropdown.

Thanks

#577235

Minesh
Supporter

Languages: English (English )

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

Well - you should just add one select option before the loop. Please check following code with my comment "change start" and "change end".

//dynamically populate select field from Types
add_filter( 'wpt_field_options', 'prefix_custom_options', 10, 3);
 function prefix_custom_options( $options, $title, $type ){
    switch( $title ){
        case 'working at member':
            $options = array();
            $args = array(
                'post_type'        => 'post',
                'post_status'      => 'publish');

            $posts_array = get_posts( $args );

/*  Change start */
              $options[] = array(
                    '#value' => 0,
                    '#title' => "Select One option",
                );

/*  Change end */

            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                );
            }
            break;
    }
    return $options;
}

Have a great day!! 🙂

#577237

Thanks 🙂

#577241

Hi Minesh,

To display selected company name below user I have written following code.

[types usermeta='working-at-member' format="name"][/types]

But, It is displaying id of "member".

How to display company name of user?

Thanks