Skip Navigation

[Resolved] Link to parent relationship not working in grid

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

Problem:
How to output a field from a parent post when displaying posts using the Essential Grid plugin?

Solution:
To display fields from the post itself you can use Views shortcodes in this format:

[wpv-post-title id='%post_id%']
[php]

To display a field from a related post (e.g. parent) it is necessary to create a custom shortcode that uses the relationships API to query the related post, like this example for a post link:

[php]
add_shortcode( 'related_link', function( $atts ){
 
    $pid = $atts['id'];
 
    $related_id = toolset_get_related_post( $pid, "artist-release", "parent" );
 
    $output = wpv_do_shortcode( "[wpv-post-link id='" . $related_id . "']");
 
    return $output;
});

which you can then insert into a template, like so:

[related_link id='%post_id%']
This support ticket is created 6 years, 6 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 11 replies, has 2 voices.

Last updated by chrisC-25 6 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#950374

I had a similar problem with a past grid plugin info found here:
https://toolset.com/forums/topic/i-want-to-pull-metadata-into-a-grid-plugin-pulling-from-parent-relationship-wa/
I am now trying with another grid plugin.

Update to this topic. I am now using another Grid called Essential Grid and I am confused by what to put in there based on the new Toolset Parent/Child relationship. Here is the page:
hidden link

Here is the code I am trying:
[wpv-post-link item=%@artist-release.parent%]
[wpv-post-link item=%_wpcf_belongs_@artist-release.parent%]

They both link back to the page "releases" but not back to the Parent "Artist" as I want.

Here is some background on the plugin and the use of meta:
hidden link

#951106

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Chris

The correct format for linking to a parent post from the child in an artist-release relationship would be

[wpv-post-link item="@artist-release.parent"]

The link you shared to the essential grid faq is down so I couldn't read it, but I'd say the above either works or it doesn't.

I'm guessing that Essential Grid uses a custom query to retrieve the content, and the key would be whether it sets the global $post object as it iterates over the posts.

The use of post meta isn't particularly relevant because the relationships are now stored in custom database tables, not in post meta.

Displaying fields of related posts works in grids you make with Views, but it may or may not when using other plugins to create a grid.

#951471

Thanks, if I try to just add the shortcode it does not work and in fact the grid refuses to load. That is why we used meta in the other grid which is what I am trying with this:
[wpv-post-link item=%_wpcf_belongs_@artist-release.parent%]

Looks like that link is working again, can you please take another look?

Thanks

#952222

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You would only be able to use post meta for referencing the related post if you were using relationships created in Types 2 and which you have *not* migrated to Types 3.

The relationships in question, are they Types 2 or Types 3 relationships?

#952319

It is a Types 3. So basically I can never upgrade my other site because it relies heavy on this function and now I cannot get this to work on this site because it is Types 3? That is not good.

#952354

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Chris

If you have this working with another plugin on a site that uses Types 2 (where relationships are set by post meta) then I can try and help get it working with Types 3, although you should note that if you update to Types 3 but don't run the migration wizard then it should continue to work as before, you might want to try that on a staging site first.

Otherwise, please give me details of how you have it set up when it is working and I'll try and set up a simple site that does the same, then do some testing.

#952392

The details of how it is working on the other site are here:
https://toolset.com/forums/topic/i-want-to-pull-metadata-into-a-grid-plugin-pulling-from-parent-relationship-wa/

But that is with a different plugin. However the theory should be the same. The new plugin I am using is Essential Grid.

Some info on that is here:
hidden link

Using this almost works but does not find proper parent:
[wpv-post-link item=%_wpcf_belongs_@artist-release.parent%]

Thanks

#953581

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Chris

If your site where it works is built with Types 2 (so that relationships are stored as post meta) and a different grid plugin, then that is very much separate to getting it to work with a new grid plugin on a site where the relationships are built with Types 3.

For the original site, you should be able to upgrade to Types 3, but should *not* run the migration wizard to convert relationships, in which case it should continue to work as before.

Going back to using the Essential Grid plugin with relationships created in Types 3, it may or may not be possible.

My initial tests suggest not.

I've spent some time testing Essential Grid and I can see that you can set the source as Posts to be able to insert fields from posts (e.g. the post title, but also post meta), or you can insert text/html.

It seems like the only way for this to work is if Essential Grid allows you to add shortcodes to this text/html editor.

I tried that and had partial success.

If I register a custom shortcode [boo] to output "Boo!" then that works.

When I try to use Views shortcodes they don't.

The problem is one of context.

The Views shortcodes expect the global $post object to be the current post in the loop (i.e. each item in the grid), but I checked and the $post object is the page where the essential grid shortcode is inserted.

So the Views shortcodes have the wrong context.

It may be possible to work around this problem, but we need to know from Essential Grid if there is a global object which refers to the current item in the grid in this context. We are looking for an equivalent to the global $post object in the loop for the custom query they run to generate the grid.

If you can contact them and find an answer to that, I can try and help get this working.

#953694

Thanks Nigel. Can we add the shortcode via the function.php file as suggested here:

hidden link

Or is that what you tried with the [boo] shortcode?

#954403

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, so from that documentation it is possible to pass the id of the current post, so you can use Views shortcodes like this:

[wpv-post-title id='%post_id%']

That will output the post title of the current post for each post in the grid.

But it won't work for getting fields from related posts.

I experimented with what I thought was a clever workaround to temporarily set the global $post context before parsing the Views shortcodes that specify a related post as source but it didn't work.

So, the conclusion is that you can output fields of the "current" post, but not from related posts.

It would be possible to do that if you registered a custom shortcode that passed the id as above and which then retrieved the related post using the Toolset relationships API (https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post) and then returned the required field as a string.

You would need something like this:

add_shortcode( 'related_link', function( $atts ){

	$pid = $atts['id'];

	$related_id = toolset_get_related_post( $pid, "artist-release", "parent" );

	$output = wpv_do_shortcode( "[wpv-post-link id='" . $related_id . "']");

	return $output;
});

Then where you insert the shortcode into Essential Grid you need to pass the id, like so:

[related_link id='%post_id%']

The above code is a little crude with the slug hardcoded etc. and can be improved upon, but it should get you started.

#956542

Thanks Nigel! That shortcode solution worked!

#1142432

Hi Nigel,

Following up on this after sometime. I would like this solution to work with a different Grid plugin as Essential grid is quite slow. The Grid by Theme One is what I want to use. Documentation here:
hidden link

The shortcode does not work in this setting. I have tried several workarounds to no avail.

The error says:
All provided arguments for a related element must be either an ID or a WP_Post object.

Any insight would be greatly appreciated!