I have a custom post type "agents" which is related to the custom post type "properties." Each property has agents related to that property. I currently have a view set up to populate the agents associated with each property which displays a photo and information of that agent on the property.
Is there a way to sort the view output of the agents on the property? Properties can have multiple agents related to the property, which the view takes care of outputting. However, I also need to then sort the view of those agents by main listing agent, secondary listing agent and so on. So on the backend....when filling out the property, the agents would be connected to the property but then have a custom field to select primary, secondary, etc. & then filter the view output to be sorted by primary, secondary, etc.
Initially I created post reference custom fields for "Primary Agent", "Secondary Agent" etc., but ordering by those post reference fields in the view outputs "no items found."
Thanks!
Hi, the post reference field is not a bad approach here. Instead of using a View to display the related Agents, you would just use the post title shortcode. When inserting the post title shortcode in a View's loop, you can use the Post Selection tab to choose a post related to the current post by a reference field (see the screenshot). So basically you would insert the post title shortcode as many times as there are levels of Agents. Let's assume it's 3 levels but it could be more. The system will generate the appropriate shortcodes to display each agent, something like this:
Primary Agent: [wpv-post-title item="@reference-slug-1.parent"]<br />
Secondary Agent: [wpv-post-title item="@reference-slug-2.parent"]<br />
Tertiary Agent: [wpv-post-title item="@reference-slug-3.parent"]<br />
Your slugs will probably be different from mine, and depending on how you set up your post references you may need to use ".child" instead of ".parent". You could also use conditional HTML to hide each line of results if no agent is selected for that level:
[wpv-conditional if="( '[wpv-post-id item="@reference-slug-1.parent"]' ne '' )"]
Primary Agent: [wpv-post-title item="@reference-slug-1.parent"]<br />
[/wpv-conditional]
[wpv-conditional if="( '[wpv-post-id item="@reference-slug-2.parent"]' ne '' )"]
Secondary Agent: [wpv-post-title item="@reference-slug-2.parent"]<br />
[/wpv-conditional]
[wpv-conditional if="( '[wpv-post-id item="@reference-slug-3.parent"]' ne '' )"]
Tertiary Agent: [wpv-post-title item="@reference-slug-3.parent"]<br />
[/wpv-conditional]
The other option is to use a many-to-many post relationship between Agents and Properties, and use an intermediary post type in the relationship. Then you could apply a custom field to the relationship, where you would set the level of each Agent related to the Property using a number. Then you could sort a View of the intermediary post type by the custom field number. This approach is a bit more complex and has some significant impacts for searches (you can't search or filter a View of Intermediary posts based on custom fields in other post types). So I think your post reference fields approach is probably more practical, because it doesn't have that major limitation.
Thanks Christian, appreciate the options you've laid out.
I agree that for choosing the primary agent, secondary agent, etc. the post reference is the best to sort on the frontend. The only issue which I probably should have mentioned is that I am pulling other custom fields for the Agent post type as well.
While I can pull the title (Agent Name) and Featured Image (Agent Photo) using post reference, how would I then pull custom fields from that post type that include contact info like phone number and email address. Basically, this is why I chose a view so that these fields would be shown for each agent and styled.
Each agent on the property needs the output of Featured Image, Agent Name, Phone #, Title & Email Address. The actual post type of "Agent" does not need to be archived or even linked as there are current agent bio pages on the site already. The post type of agent was created simply for the relationship and the ability for an admin to choose the agents on the backend and then all the information for each agent to be outputted on the front end and styled when they are adding a new property.
I've attached the current view used for the output, and basically just need to then sort the order of the agents on backend for frontend output depending on which is the main agent, secondary agent, etc. The layout is great, its just the sorting of the agent "blocks" (Name, Photo, Contact info) that needs to be controlled on a per property basis.
Again, really appreciate the help!
While I can pull the title (Agent Name) and Featured Image (Agent Photo) using post reference, how would I then pull custom fields from that post type that include contact info like phone number and email address.
The method to display custom fields of a related post is essentially the same as the method to display the title and featured image of the related post. You use the Fields and Views button to insert the custom field, and use the Post Selection tab in the popup to select a related post by post reference. The system will generate the shortcode automatically. Or you can manually add the item attribute to the shortcode if you know the correct syntax:
[types field='bio-link' output='raw' item="@reference-slug-1.parent"][/types]
Perfect, thanks Christian, I was able to implement directly into the content template manually using the the correct syntax. The custom fields for the Agent type didn't give the post selection tab in the popup for the custom fields, but changing the types field in the shortcode using the specific slug worked.
Are there specific fields that you aren't allowed to select "related post by post reference" on a custom post type? Just wanted to double check if that was a setting in the post type somewhere that hindered it.
Thanks again!
I'm not sure offhand why this would be happening, as far as I know it's not dependent upon the field type. It's more dependent upon the context where this Content Template is in use. For example, in a View of taxonomy terms, post selection is not applicable. If you want to show me on your site where I can see this in action, I'll be glad to take a closer look. It could be a bug, or it could be the expected behavior. Please provide login credentials in the private reply fields here and let me know how to trigger the dialog you mentioned.
Okay I see, this behavior is specific to the Divi builder. I'm aware of a similar issue with RFG fields not appearing, but that was resolved in the recent releases. Let me ask my 2nd tier team about this. In the meantime, it sounds like the workaround of manually adding the item attribute is working, so that's good news.
Appreciate it Christian, I had a hunch it was related to the divi builder. And yes, in the meantime, manually adding the attribute is working just fine so I'll continue doing that until there's a fix.
Thanks again!
Just a quick note that my second tier team has confirmed the issue with selecting related posts in Divi Content Templates and we've published an erratum post here: https://toolset.com/errata/the-options-to-select-custom-fields-of-related-posts-dont-appear-when-the-content-template-is-being-designed-with-divi/
I'll keep you posted as work progresses.
Hi, I wanted to give you a quick update to let you know our developers plan to include a fix for this issue in the upcoming Views 2.8.4 release. We don't have a date finalized just yet, but I will let you know when I have more information about the release schedule.
Hi, Views 2.8.4 is now released and available for automatic update or download from https://toolset.com/account/downloads. Please update your Toolset plugins to the latest versions and let me know if this problem is not completely resolved. Thanks for your patience!