Skip Navigation

[Resolved] Post relationships with Views

This support ticket is created 10 years, 9 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.

This topic contains 14 replies, has 3 voices.

Last updated by Paul 10 years, 9 months ago.

Assisted by: Bigul.

Author
Posts
#38407
screenshot_02.jpg

Hi there. I am struggling with something at the moment so hopefully you can help further.

-> I have created a Real Estate site and have setup Custom Post Types for Agents and Properties
-> I have set post relationships between the two...Agents are a parent of Properties
-> I have used a Hook in functions.php so that when a logged in user uploads a property via a CRED form, a custom field in Properties CPT gets set to that User Name (of the logged in agent)

What I want to do now is display a View on each property page with the relevant Agents details. I also want to list that Agents other properties on the same page.

I'm guessing I need to setup a filter that checks for that User Name and associates it with the custom field in the Property CPT so I can match the two.

I guess my main problem is linking a user, to their CPT entry, to the Property they uploaded - i.e. Relationships

PLEASE SEE ATTACHED IMAGE -> I've noticed at the bottom of each Property page in WordPress there is a Select menu that says Post belongs to. Is there a way of updating this, maybe via a hook, each time a property is uploaded to the relevant user/agent?

Thanks

#38414

****MORE INFO****

This is what I have in my functions.php:

add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==35)

{
$current_user = wp_get_current_user();

add_post_meta($post_id, 'wpcf-agent-id', $current_user->user_login, true);
}
}

Form 35 is the CRED Property Upload form. The above works great. But maybe I have to modify this to set Post Belongs to .....the user.

To help myself out, I have made the Usernames in WordPress exactly the same as the Agent CPT Field - Agent Name.

Thanks

#38544

Bigul
Supporter

Hi Paul,

Hope post_title(Agents) is the same as the user_login then it will be easy. If not,it can be a bit more complicated.

Hope you can solve the issue by adding the following code in hook.

add_post_meta($post_id, '_wpcf_belongs_post_id', $agent_id, true);

$agent_id will be the ID of Post Agent Id of log-in user.

---
With Regards

Bigul

#38566

Hi there- thank you for your input. However, this doesn't seem to be updating the correct field.

What does $agent_id refer to by the way?

I've tried the following:

add_post_meta($post_id, 'wpcf-agent-id', $current_user->user_login, true);
add_post_meta($post_id, '_wpcf_belongs_post_id', $agent_id, true);

and

add_post_meta($post_id, 'wpcf-agent-id', $current_user->user_login, true);
add_post_meta($post_id, '_wpcf_belongs_post_id', $current_user->user_login, true);

Neither of which are updating the select menu on the actual post which seems to be called: wpcf_pr_belongs[agents]

Any ideas?

Thanks

#38569

Bigul
Supporter

Hi Paul,

Sorry for the confusion. It will not be a User Id.

Properties Posts will be the child of a Agent Post, am I correct ? If so you need to find the Parent Agent Id for the newly created Properties.
Hope it will works.

---
With Regards

Bigul

#38582

Hi there,

Yes that is correct - Properties posts are children of Agents. The CPT is called Agents. Does the $agent_id refer to this so I just have to change it to $agents_id?? I am actually using user name rather than user ID (which refers to a number)...

Can you help a little further - almost there with this one!

Thanks

Paul

#38591

Bigul
Supporter

Hi Paul,

You don't need to edit, add_post_meta($post_id, 'wpcf-agent-id', $current_user->user_login, true);. Because your user name and Agent Post name is same.

I have one doubt. Who is creating the "Agent" post entry and what will be the user role for "Agents" ?

If a user(Agent) has only one post(I mean Agents Post entry), then write a query to find the Agent Post entries ID(Entry of relevant Agent) and assign to child Properties when creating it. Hope it will work.

For your information in latest Views release we have added filter by Author feature.

Please let me know your feedback.

---
With Regards

Bigul

#38595

Hi there,

-> I have used Access to setup a role for Agent.

-> Each Agent has one single Agents CPT post.

-> The link relationship is between Agents CPT and Properties CPT.

-> I have setup new users, 1 for each agent, using exactly the same name as apears in Agents CPT field 'wpcf_agent-name'

-> When the agent logs in using their individual agent credentials, I allow them access to a CRED form which creates a Properties Post

All I really want to do is make sure that the current user (i.e. Agent 1) is linked to their Agents CPT post. Then any properties uploaded will automatically be assigned to that Agent CPT post....

Can you be of further assistance now with that info above?

Very very much appreciated! (I'm not a programmer btw but am able to do some of the Hook stuff!)

Paul

#38606

Bigul
Supporter

Hi Paul,

"All I really want to do is make sure that the current user (i.e. Agent 1) is linked to their Agents CPT post. Then any properties uploaded will automatically be assigned to that Agent CPT post…." . Please try it with CRED hooks(cred_submit_complete or cred_save_data). While creating a Property Post. We are happy to help you.


With Regards

Bigul

#38616

Juan
Supporter

Timezone: Europe/Madrid (GMT+01:00)

Hi Paul.

I think I may be able to help you with this. What you want to do is quite simple, but depends on the way you have added your Agents custom posts.

Firts, let me explain what Bigul meant. You have two Custom Post Types, Agents and Properties. Agents is parent of Properties. To store relationships between parents and children, we use a custom field in the child that stores the ID of the parent, using the parent Custom Post Type slug as part of the key. So in a Property, the custom field "_wpcf_belongs_agent_id" should store the ID of the Agent you want to add as parent of the Property. Do you follow me?

Now, you are creating a custom field on Property creation (using CRED) that stores the user_login of the user that created the Property.

You see: we only need a way to find the relation between the user that creates the Property and his ID as Agent post type, and add this ID as a custom field to the Property using the key I wrote before.

So the relevant question is: how are Agents named/stored? Do they use as post_title anything that might be accessible in their user profile screen as WordPress users? Or can you add a custom field to every Agent that stores something accessible in the WordPress profile screen of any author? Depending on the answer, this could be quite simple to achieve.

I hope it all makes sense to you.

I'll be waiting for your response.

Regards,
Juan de Paco

#38624

Juan my friend, you have described exactly my problem!!

quote..."You see: we only need a way to find the relation between the user that creates the Property and his ID as Agent post type, and add this ID as a custom field to the Property using the key I wrote before."

Do I do this in my hook that I wrote above (#38566)?

I can provide login details to my website for you to take a very quick look at and see my setup?

Thanks you once again for your assistance...

Paul

#38631

Juan
Supporter

Timezone: Europe/Madrid (GMT+01:00)

Hi Paul.

Yes, you should find the Agent ID in the my_success_action() function using the data provided by the $current_user variable and just add that custom field I explained before (your custom field wpcf-agent-id won't be needed anymore). To do so, I just need to know how do you create Agents.

I can have a look at your installation, but I think it may be more appropriate to solve it here, where anybody on your same situation can find the answer.

So please describe here how yo create an Agent post type. Do you use as post_title the name, the full name, the email? We could create a custom field in Agents and add there something like the user_login, and then find the agentID by looking for an Agent with this custom field the same as the user that creates the Property. We have plenty of options, but it all depends on how do you create this Agents.

We are very close,I know. 🙂

I'll be waiting for your response.

Regards,
Juan de Paco.

#38644
Agents CPT 2.jpg
Agents CPT.jpg

Hi Depaco, we are soooo close to sloving this!!

Below are two image attachments to show you how they are created. What I'm doing is registering the user in the normal way through the backend of the site and giving them a user_name that exactly matches the Post title in Agents CPT....

Remembering that I want to allow the Agent to login using the normal wordpress credentials, once they have done this I serve them with a page with a CRED form on to upload property...

I eagerly await your response....

#38660

Juan
Supporter

Timezone: Europe/Madrid (GMT+01:00)

OK, this is going to be solved in a moment. You just need to tweak a little your function. It would be as follows. I assume your Agents Custom Post Type has a slug of "agent", so the relevant custom field name will be "_wpcf_belongs_agent_id". If this is not correct, please change it in the field name and in the get_page_by_title call.

add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==35)
{
$current_user = wp_get_current_user();
$current_username = $current_user->user_login;
$desired_agent = get_page_by_title( $current_username, OBJECT, 'agent');
if ($desired_agent) //if the Agent exists
{
add_post_meta($post_id, '_wpcf_belongs_agent_id', $desired_agent->ID, true);
}
}
}

Let me know if this works as you want it to.

Regards,
Juan de Paco

#38669

WORKS WORKS WORKS!

Thanks you so much for all your time and input!

I'm wondering whether this kind of User processing will eventually find its way in to the Toolset Core...because the ability to match user registration with CPTs and what we've done here would make Toolset pretty much indispensable for most web builds!

Thanks again,

Paul

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.