Skip Navigation

[Resolved] Displaying child custom post (one-to-one relationship) in Elementor

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

Last updated by filipeC 1 month ago.

Assigned support staff: Nigel.

Author
Posts
#1684461

I set up a child post (hull analysis) with a one-to-one relationship to a parent post (shipwreck). The form to create the child post (Add Hull Analysis) is working, and it successfully establishes the parent-child relationship (the View I created pulls up the child post without a problem). My template was built in Elementor, and my forms are in pages with a url ending in ?shipwreck_id=#.

I need to do a couple things that I can't get working:

1. I need to display the link to the Add Hull Analysis form only if there is not already an existing Hull Analysis child post (and likewise display the Edit Hull Analysis link and View showing the Hull Analysis if there is an associated child post). I can't get the PHP code to work, which is weird because I got a very similar code to work just fine on a View created by a repeatable group (https://toolset.com/forums/topic/edit-post-link-when-using-an-elementor-template/)

$hullanalysisids = toolset_get_related_posts( 
      get_the_id(),
      array( 'shipwreck', 'hull-analysis'),
      [
        'query_by_role' => 'parent',
        'role_to_return' => 'child',
        'need_found_rows' => true,
      ]
    );
$analysiscount = $hullanalysisids ['found_rows'];
if ($analysiscount = 0) {
return true;
}

I've also tried:

$hullanalysisid = toolset_get_related_post (
    get_the_id(),
    'hull-analysis-shipwreck');
$analysiscount = $hullanalysisids ['found_rows'];
if ($hullanalysisid = 0) {
return true;
}

2. I'd like to automatically assign the title of the child post to the name of the parent post plus the words 'Hull Analysis' (e.g. 'Test Shipwreck Hull Analysis'). This seems like it ought to be simple to do, but while

value=" urlparam='shipwreck_id'

successfully passes the id number to the title field, I can't figure out how to pass the url parameter to [wpv-post-title]. I tried:

value="[wpv-post-title id="urlparam='shipwreck_id']
#1685333

Nigel
Supporter

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

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

I understand you want to conditionally display a link to an add post form where there is no existing related post or to an edit form when there is already a related post, is that right?

You can use a View for this, though you may find it easier using the legacy Views editor rather than Blocks.

A View queries for posts and determines what the output should be if it finds some but also what the output should be if it does not.

So you can create a View to query the related post type which includes a relationship filter to only return posts related to the current post.

In the output of that View for when a result is found you will insert the edit post link. In the section for when no results are found you will add a link to add a new post.

If using the legacy Views editor you would add those links in the spaces shown here:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
          <!-- LINK TO EDIT POST HERE -->
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		  <!-- LINK TO ADD NEW POST HERE -->
	[/wpv-no-items-found]
[wpv-layout-end]

In the second question, you are trying to include the title from the origin post when adding a new post, is that right?

I think you need something like

[cred_field field='post_title' class='form-control' output='bootstrap' value='[wpv-post-id id='[wpv-search-term param="shipwreck_id"]'] Hull Analysis']

You may have to tweak the mix of single and double quotes for the value attribute.

#1685459

1. That's a good idea but it actually won't work for what I'm trying to do because I also need to also add a condition for whether the user can edit the post. I need it to do the following:

(a) If the user doesn't have permission to edit the post and there is no data in the child post, nothing at all shows (the view itself is completely hidden).

(c) If the user has permission to edit the post, and there no data in the child post, the view shows the 'create child link.'

(b) If there is data in the child post, a the view displays a link to the child post regardless of whether the user is logged in.

Its easy to hide the view if the user does not have permission to edit the post using the Elementor Addon plugin Dynamic.ooo, but to make the visibility also conditional on whether there is data in the child post, I need the php snippet above to work.

2. It is not reading more than two sets of quotes as nested within each other.

This code returns the value I want:

[wpv-post-title id="[wpv-search-term param='shipwreck_id']" output="sanitize"]

But I cannot nest that inside the cred_field, because it reads this:

[cred_field field='post_title' class='form-control' output='bootstrap' value='[wpv-post-title id="[wpv-search-term param='shipwreck_id']" output="sanitize"]']

And returns what is shown in the screenshot

#1685621

1. I figured this one out, I was missing an equal sign in my conditional. The code should have been:

$hullanalysisids = toolset_get_related_posts( 
      get_the_id(),
      array( 'shipwreck', 'hull-analysis'),
      [
        'query_by_role' => 'parent',
        'role_to_return' => 'child',
        'need_found_rows' => true,
      ]
    );
$analysiscount = $hullanalysisids ['found_rows'];
if ($hullanalysisid == 0) {
return true;
}
#1685725

3. I'm having some formatting problems with the View I created with the Create/Assign/Edit links:

(a) The top button (see screenshot) is not taking the formatting. The code works fine if the button is added to the loop that displays when there IS a Hull Analysis child post displayed, but won't take formatting when it is added to what to display when there ISN'T an associated post.

(b) Also the form to assign the an existing hull analysis is not taking the parent (shipwreck) from the filter.

[wpv-no-items-found]
		<div class="row " style="background-color:#F0F5F5; padding:25px 5px 0px 5px; margin-left:20px">
          <div class="col-md-12">
			<h4>Create a New Hull Analysis</h4>

            <!-- wp:toolset-blocks/button {"style":{"fontSize":16,"backgroundColor":{"r":9,"g":38,"b":53,"a":1},":hover":{"backgroundColor":{"r":52,"g":217,"b":232,"a":1},"textColor":{"r":9,"g":38,"b":53,"a":1}},"display":true},"dynamic":{},"buttonText":"Add Hull Analysis","icon":{"cssUrl":"<em><u>hidden link</u></em>","fontCode":"","fontFamily":"dashicons","position":"left","spacing":0},"url":"edit-shipwreck-pg6/?shipwreck_id=[wpv-post-id]"} -->
<div class="tb-button" data-toolset-blocks-button="1"><a class="tb-button__link" href="edit-shipwreck-pg6/?shipwreck_id=[wpv-post-id]"><i class="tb-button__icon"></i><span class="tb-button__content">Add Hull Analysis</span></a></div>
<!-- /wp:toolset-blocks/button -->
            
			<strong>[wpml-string context="wpv-views"]<a href='edit-shipwreck-pg6/?shipwreck_id=[wpv-post-id]'>Click to Create Child</a>[/wpml-string]</strong>
			<h4>Or, Assign an Existing Hull Analysis to this Shipwreck</h4>
			<p>[cred-relationship-form form='assign-hull-analysis-to-shipwreck' parent_item='$fromfilter']</p>
            
          </div>
		</div>
	[/wpv-no-items-found]
#1686249

Nigel
Supporter

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

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

Regarding the formatting, you are adding buttons with the block editor somewhere, then copying and pasting the HTML (including those block related comments) into the View output editor, both in the items found section and the no items found section, is that right?

I tested doing just that on my own test site, and it works okay, but this is on a simple site that doesn't involve Elementor. I just copied and pasted the relevant markup into the classic View output editor and checked what happened where the View had results to show and no results to show, and in both cases the formatting was respected.

I would test inserting your View on a page that isn't designed with Elementor, to isolate the problem.

Regarding pre-selecting the shipwreck, you insert the relationship form directly in the output of the View (rather than linking to it elsewhere), right? I don't recognise the $fromfilter attribute value, I don't know where you got that from.

The problem here is that you are in the no-items-found section of a View, so there is no current post to use as a reference or a starting point. But this View itself is being displayed for a single Shipwreck post, correct? I'm not entirely sure it will work in this context, but you want your parent item to be the current shipwreck post being displayed, and I think you should be able to use $current to provide the attribute value (instead of $fromfilter).

Could test that and let me know if it works? If not, I'll try and find something that does.

#1686813

2. Did you have any thoughts on automatically assigning the title to the post during creation by CRED form?

(from above:) It is not reading more than two sets of quotes as nested within each other.

This code returns the value I want:

[wpv-post-title id="[wpv-search-term param='shipwreck_id']" output="sanitize"]

But I cannot nest that inside the cred_field, because it reads this:

[cred_field field='post_title' class='form-control' output='bootstrap' value='[wpv-post-title id="[wpv-search-term param='shipwreck_id']" output="sanitize"]']

And returns what is shown in Screenshot 1. It seems to be splitting the code at the innermost quote, so that it's reading these two as separate lines.

'[wpv-post-title id="[wpv-search-term param=' 
shipwreck_id']" output="sanitize"]'

Is it possible to define a term somewhere in the form and use it to set the id? If so, where do I put the definition of the attribute?

Something like: $shipwreckid=[wpv-search-term param='shipwreck_id']
So that I can use it in the cred field like:

[cred_field field='post_title' class='form-control' output='bootstrap' value='[wpv-post-title id="$shipwreckid" output="sanitize"]']

3. (a) Yes, it works correctly when inserted into a page not controlled by Elementor. The View is being displayed by the Toolset-created "element" (Elementor equivalent of a block) for adding Views to Elementor pages. Adding a Toolset button block like this actually breaks the styling for the rest of the page.

Edited to add: Wow, I over complicated this. All I needed to do was add class=button inside my link

<a class="button" href='edit-shipwreck-pg6/?shipwreck_id=[wpv-post-id]'>Create Hull Analysis</a>

(b) The $fromfilter was inserted by the wizard when I added the form to the view (see Screenshot 2). The $current filter successfully sets the test, so that solves this problem for me.

#1686893

Nigel
Supporter

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

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

2. I stumbled across something while I was testing this.

It was working in my initial tests, then stopped working as I was cleaning up the form.

The trick to getting it to work was to include the wpv-post-title shortcode somewhere in the form before I came to use it to provide a value for the value attribute of the cred_field shortcode, so this failed:

[cred_field field='post_title' class='form-control' output='bootstrap' value='[wpv-post-title id="[wpv-search-term param='shipwreck_id']"]']

Whereas this worked:

[wpv-post-title id="[wpv-search-term param='shipwreck_id']"]
[cred_field field='post_title' class='form-control' output='bootstrap' value='[wpv-post-title id="[wpv-search-term param='shipwreck_id']"]']

Naturally, you could use CSS to hide the first time it is output.

I can only assume that there is some caching of the shortcode at work that explains why the second version works and the first does not.

I was testing on a basic site, not with Elementor, if you want to try the same on your site.

3(a): OK, I will have to set up a test with Elementor and try again.

3(b): Right, thanks, I haven't noticed that before, it must be specifically when you insert the form in the output of a View with a corresponding relationship filter.

#1689265

I can work with that. Thanks for your help!