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.
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.
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]})]
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!
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!!
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...
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.