Skip Navigation

[Resolved] _wpcf_belongs_ not working in new version

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

Problem: I would like to use a conditional to test whether or not a parent post exists for the current post (child) in a specific post relationship. In the past, I had a conditional that worked well by testing the hidden custom field _wpcf_belongs_restaurants_id on the child post, but it seems this is no longer working. The custom field does not exist in the database for the child post.

Solution: After migrating post relationships, the _wpcf_belongs_{parent-post-type-slug}_id custom field value is no longer reliable for determining the parent post. Post relationships now rely on custom database tables to establish relationships, so conditionals testing the old hidden custom field are no longer valid. Instead, you can test for the post ID of the parent post in a conditional using the wpv-post-id shortcode and item="@relationship-slug.parent" syntax:

[wpv-conditional if="( '[wpv-post-id item="@relationship-slug.parent"]' ne '' )"]
  
parent exists
  
[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/

This support ticket is created 3 years, 1 month 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 5 replies, has 2 voices.

Last updated by nedG 3 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1996281
vabeach-event-instance.jpg
vabeach-view.jpg

I have a complex site that displays event. I have the following CPT relationships...

Event Instances (this CPT simply holds the START DATE and END DATE of the event)

"Event Instances" belongs to the CPT "EVENTS"
EVENTS holds all of the text description and information about the Events.

"Event Instances" also belongs to possibly one of several other CPTS depending on where the event is being held. For example, if it is being held at a Restaurant, then it belongs to the RESTAURANTS CPT. If it is being held at a local Event Venue then it belongs to the EVENT VENUES CPT... and so on

I have had a View working for several years that worked well for this.. but all of a sudden it has stopped working (possibly due to an update?)
Here is a sample of my code...

[code]
[wpv-conditional if="( ($(_wpcf_belongs_restaurants_id) ne '') AND ($(_wpcf_belongs_restaurants_id) ne '0') )"]
<div><i class="fa fa-map-marker"> [wpv-post-link id="$restaurants"]<br> <small>[types id="$restaurants" field="listing-street-address"][/types] [types id="$restaurants" field="listing-city"][/types] [types id="$restaurants" field="listing-state"][/types] [types id="$restaurants" field="listing-zip"][/types]</small></div>
[/wpv-conditional]

[wpv-conditional if="( ($(_wpcf_belongs_event-venue_id) ne '') AND ($(_wpcf_belongs_event-venue_id) ne '0') )"]
<div><i class="fa fa-map-marker"> [wpv-post-link id="$event-venue"]<br> <small>[types id="$event-venue" field="listing-street-address"][/types] [types id="$event-venue" field="listing-city"][/types] [types id="$event-venue" field="listing-state"][/types] [types id="$event-venue" field="listing-zip"][/types]</small></div>
[/wpv-conditional]
[/code]

Unfortunately after analyzing the metadata for the EVENT INSTANCE post, it appears that the _wpcf_belongs_{parent-slug}_id values don't exist anywhere in the metadata. It has vanished from the metadata and therefore is "blank" for all conditional statements.

Please help solve this or else help me find a better way to display the data.

#1996549

Hi, the new post relationships system in Types 3.0+ no longer implements post relationships using the old _wpcf_belongs_{parent-post-type-slug}_id postmeta value. Prior to updating the post relationship system, a postmeta key was added to the child post. That postmeta key used the syntax _wpcf_belongs_{parent-post-type-slug}_id, and the value was the parent post ID. After updating, new database tables are added and the relationships are migrated to the new system. In the new system, there is no postmeta key involved and you must use the Post Relationships API or Views to determine whether or not a post is related to another post in a specific post relationship.

If you go to Toolset > Relationships you can determine whether or not your site has migrated to the new post relationships system. You will either see a message prompting you to migrate to the new post relationships system or you will see a standard dashboard of all the post relationships existing in your site. Which do you see at Toolset > Relationships?

If you see the dashboard with a list of post relationships, it means your site has migrated to the new post relationships system. If you see the prompt to migrate, it means your site has not yet migrated. My guess is that your site has migrated to the new post relationships system, hence the postmeta key approach for determining whether or not related posts exist will no longer be effective.

Let me know what you find out and we can discuss next steps for updating the code here.

#1996599

It appears as though all of the Relationships have migrated.

That is very strange.. because I could have sworn that all of this has been working for at least the past year (well after your upgrade to Types 3.0+)

So how can I get this Conditional statement to work? ... aka... how do I determine if my Intermediary post a child of another post in a CPT?

#1996601

Ahh.. I think I have found it...
Is this the answer..
https://toolset.com/forums/topic/check-if-parent-post-exists/

#1996641

That is very strange.. because I could have sworn that all of this has been working for at least the past year (well after your upgrade to Types 3.0+)
There is some backwards compatibility built into the system, so it is possible that conditionals testing older post relationships established before the 3.0 update would continue to work in some cases with the old syntax if the old postmeta relationship keys still exist in the postmeta table. However, new relationships established between posts after migration will not include the same postmeta value, as you have seen, so the conditional would not work in those cases. I suspect you could have been seeing remnants of old relationships working, and now newer cases do not include the old fallback syntax so you see them failing. Just a guess.

So how can I get this Conditional statement to work? ... aka... how do I determine if my Intermediary post a child of another post in a CPT?...Is this the answer..https://toolset.com/forums/topic/check-if-parent-post-exists/
Yes, testing the post ID shortcode using the @relationship-slug.parent identifier in the item attribute is a valid approach for determining whether or not a parent exists in a specific post relationship. It should work with relationships that were migrated, as well as relationships established after the migration took place.

#1996669

Thanks Christian! I should have looked around more closely!

As always, great support!

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