Skip Navigation

[Resolved] automatically linking child posts during WP-AllImport Pro

This support ticket is created 7 years 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 6 replies, has 3 voices.

Last updated by Cam Macduff 7 years ago.

Assisted by: Luo Yang.

Author
Posts
#589282

I am importing a child post type and would like to have these posts link to their proper parent during import. I've created both the parent and the child posts and custom fields with WP-Types. I have successfully imported records for both custom post types. Both the child and parent posts will have the same title.

WP-All Import uses place holders for identifying the unique field during import, But only allows me to insert one of the imported fields into _wpcf_belongs_parent_id

There is a spot on the import interface where I can run PHP functions during the import and call them from the input field. What I need is a function that can return the post_ID of the parent post with the same title as the record I'm importing. I will then have the correct post_ID in the _wpcf_belongs_parent_id fields.

#589351

Dear Patrick,

Yes, you are right, you will need to setup the custom feild "_wpcf_belongs_parent_id" value for each child post.
You can try with wordpress function get_page_by_title() to get the post ID by post title, see wordpress document:
https://codex.wordpress.org/Function_Reference/get_page_by_title
Retrieves a post given its title.

#589572

Thank You, Lou,

I did figure this out & I'm posting here for anyone else who might need to know.

This is the function I used in the Function Editor:

<?php
function get_parent_id( $sample_id ) {
	$slug = strtolower( $sample_id );
    $query = new WP_Query(
        array(
            'name' => $slug,
            'post_type' => 'sample'
        )
    );
    $query->the_post();
    return get_the_ID();
}
?>

This is how I entered it into the _wpcf_belongs_sample_id field:

[get_parent_id({sampleid[1]})]
#590510

OMG! I love you! I've been trying to solve this for weeks on my own!
Do I create the field '_wpcf_belongs_sample_id' as a custom field in AllImport or in Toolset?
Do I need to modify / replace '$sample_id' with anything? Eg. that's just not a placeholder?
Apologies for stupid questions in advance!

#590518

I should clarify.
I'm importing two different record sets each day to two different post types.
One parent post (Vet Practices) the other a child post (vets).
The only common field between the two imports is 'Practice Name' and that's the key to link the right vets to the right practice.
How would I change your script to make it work for my situation?
I'm really surprised this hasn't come up a lot more on here!!

#591107

Any chance you can give this some thought or comment Patrick? I feel like your solution is just a hairs breath away from what I need to do...

#591136
Screen Shot 2017-11-20 at 10.48.39 PM.png

See attached.
This is as far as I managed to get and I'm not even sure I'm update your script correctly...
Anyway, it doesn't seem to be working.