Skip Navigation

[Resolved] Code snippet to get the Post ID of a parent post via child Post Reference field?

This support ticket is created 3 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Tagged: 

This topic contains 11 replies, has 2 voices.

Last updated by PaulS4783 3 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#1953005

I have two post types.
Clients
Vehicles

One Client may own many vehicles.
The Vehicle table includes a POST REFERENCE FIELD which references the post title of the Client post.

How can I write a code snippet that retrieves the Post ID of the parent post?
When the vehicle post is saved, I want to save that value into a custom field of the Vehicle post.

#1953235

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You mean, when you save the vehicle post from the backend admin?

Can you please share test vehicle post link and temporary admin access details so I can review how exactly you setup the things and share the solution with you.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1953387

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I see that I do not able to add the PHP snippet using the "Custom Code" section </offered by Toolset.

It shows following notice:

Unfortunately, this snippet cannot be edited directly. Check file permissions and wp-config.php constants DISALLOW_FILE_EDIT and DISALLOW_FILE_MODS. Alternatively, you can edit the file manually.

Can you please send me FTP access details so that I can add the required code.

#1953467

If you post the code solution here, I can add it myself.

#1953471

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The thing is that I still require to build the solution and test it and then I'll send you working solution but I will require place where I can add and test solution.

That is why I'll require temporary access to FTP or you can revoke file edit restrictions.

I have set the next reply to private which means only you and I have access to it.

#1953479

If you temporarily disable the iThemes Security plugin, does that give you access to what you need?

or you can install My Custom Functions plugin for testing PHP code snippets.

It's a bit inconvenient to create an FTP account right now.

#1953563

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - when I disable that plugin, it works.

Can you please check now, I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link

function func_update_client_id_vehicle( $post_id, $post ){
    
  if ( 'auction-vehicle' == $post->post_type ) {
    
$client_id =  do_shortcode("[types field='client-id' output='raw' item='@client-username-vehicle.parent'][/types]");
     	if ( !empty($client_id)) {
            update_post_meta( $post_id, 'wpcf-client-id-vehicle', $client_id);
        }
    }
}
add_action( 'save_post', 'func_update_client_id_vehicle', 30, 2 );

Can you please confirm it works as expected.

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

#1953603

Thank you so much!
Great work.

BUT my question was actually in two parts.
I think you missed this bit from my second reply

"Actually I need the Post ID of the parent to be saved as a custom field in the Client (parent) post as well."

So currently the Client ID is entered manually in the Client Post dashboard input.

Is there a way for that field to get automatically populated with the Client Post post ID ?

#1953613

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now.

I've adjusted the code added to "Custom Code" section as given under:

function func_update_client_id_vehicle( $post_id, $post ){
    
  if ( 'auction-client' == $post->post_type ) {
    	update_post_meta( $post_id, 'wpcf-client-id', $post_id);
  } else if ( 'auction-vehicle' == $post->post_type ) {
    
$client_id =  do_shortcode("[types field='client-id' output='raw' item='@client-username-vehicle.parent'][/types]");
     	if ( !empty($client_id)) {
            update_post_meta( $post_id, 'wpcf-client-id-vehicle', $client_id);
        }
    }
}
add_action( 'save_post', 'func_update_client_id_vehicle', 30, 2 );

I hope now your both queries are resolved 🙂

#1953627

I think so.
But can you log out?

Because those records are locked with:
"AdminCols Support is currently editing"

#1953631

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - I logged out.

#1953657

Great work.

You can close the ticket.