Tell us what you are trying to do? create a child post and set multiple parents to it automatically
Is there any documentation that you are following?
https://toolset.com/documentation/user-guides/cred-forms-for-child-content
https://toolset.com/documentation/user-guides/creating-post-type-relationships
https://toolset.com/documentation/user-guides/many-to-many-post-relationship/
https://toolset.com/documentation/user-guides/creating-cred-forms
Is there a similar example that we can see?
https://toolset.com/forums/topic/set-parent-post-automatically
the similar example deal with query string and php function. in my case I would like to avoid query string if possible
What is the link to your site?
versteckter Link
I have two custom post types (parents) with custom fields that I need to call into a child post, run some calculation and save the information onto the DB.
The relationship concept I'm thinking of is one to one (parent have one child only, however the child post have many parents).
each user can create only one custom post per post type and must create the parents before creating the child post.
as the parent posts are created before the child is created I believe there is a way to get the curent user published parent post id and programmatically assign them to the child post.
login user is the posts author
I think the logic is something like:
get curent user (author) parents post types ID's
insert the parents id's into the child '_wpcf_belongs_system_id' field/s
Please advise,
thanks,
David
The relationship concept I'm thinking of is one to one (parent have one child only, however the child post have many parents).
Please acknowledge that with the stable release you can only set one parent of each type to a child Post.
Hence if you need several parents for one child, each parent needs to be a separate Post Type.
In the new Beta Types version this is different, but that Beta is to be used only for testing purposes, it features no API or integration with the rest of Toolset yet.
You can find the Beta in your Download area of Toolset.
Now, for the programmatical part, yes this is possible with some Custom Code that will use mainly the WordPress API.
Let me explain.
- you want to automatically assign the Parent Posts to a certain Child Post.
- For that you need to know that currently Types stores it's parent Post Data in a hidden Custom Field named "_wpcf_belongs_{parent-post-type_slug}_id". The value stored is the ID of the parent Post.
- Each relation has one such field. Each field holds the ID of a Post's parent.
So, you can use either the save_post() action (if in the backend) or cred_save_data() if you are in a CRED Form to hook your Custom Code at the right moment.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
Your Custom Code has to:
- get the Current User ID with the WordPress API:
https://developer.wordpress.org/reference/functions/get_current_user_id/
- get all posts of all the types you need that this user authored, with get_posts()
https://codex.wordpress.org/Template_Tags/get_posts
- since your authors will have maximally one post each parent type, you should receive here 2 values (one for each post of parent type)
- those 2 values need to be added to the post meta of the newly created or edited child posts with update_post_meta()
https://codex.wordpress.org/Function_Reference/update_post_meta
Thanks!
Hi Beda,
thanks for the information and pointing me to the
I constructed the code below but its not working.
have I overlooked something?
David
// set parent posts to a child based on current user
function set_parnet_posts($post_ID)
{
if (get_post_type($post_ID) == 'retirement') {
// get curent user ID
$current_user_id = get_current_user_id();
// get custom post a data based on based on post type and author
$args_for_budget = array(
'post_type' => 'budget',
'author' => $current_user_id,
);
$budget_data = get_posts($args_for_budget);
if ($budget_data) {
$budget_id->ID;
}
// get custom post b data based on post type and author
$args_net_worth = array(
'post_type' => 'net-worth',
'author' => $current_user_id,
);
$net_worth_data = get_posts($args_net_worth);
if ($net_worth_data) {
$net_worth_id->ID;
}
//save the parnet ids into the child post meta
update_post_meta($post_ID, '_wpcf_belongs_budget_id', $budget_id);
update_post_meta($post_ID, '_wpcf_belongs_net-worth_id', $net - worth_id);
}
}
add_action('save_post', 'set_parnet_posts', 99);
You get an array of posts, in all cases.
See the Codex:
https://codex.wordpress.org/Template_Tags/get_posts > Return Value
Hence you need to extend your logic:
if ($budget_data) {
$budget_id->ID;
}
What you try to do here is to get the property of an object from an array.
Won't' work. Instead, you need to for-each over this array ($budget_data) first.
Then, access the properties of the single object.
I suggest printing your script with some var_dump() for example.
This will tell you immediately what you have, and what you can do.
versteckter Link
Also, it is useful to turn on Debug in WordPress when you develop with PHP.
The above code used by you usually throws an error by default:
https://codex.wordpress.org/WP_DEBUG
Thanks Beda,
ok,
I have created a php test page and I test my code on it here are some of findings:
I can get the current user ID by applying the code:
$current_user_id = get_current_user_id();
echo $current_user_id;
So I know the current user ID
I can pull a list of posts based on post_type using the code:
$args = array("post_type" => "budget",);
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<h3>" . $post->ID . "</h3><br>";
echo "<h3>" . $post->post_type . "</h3><br>";
}
so I can see a list of all post types 'budget'
the problem I see is when I want to display the posts for the current user so I alter the line:
$args = array("post_type" => "budget",);
to be:
$args = array("author" => $current_user_id);
or
$args = array('author' => $current_user_id);
or
$args = array('author' => the user ID number);
or
$args = array("author" => the user ID number);
I get no results on my test page.
so for some reason the array filter does not accept the author value?
what am I missing here?
David
one more thing.
I created a simple post (I had only custom posts on the site) to see if the code will work and here is what I found:
when using the code:
$args = array("author" => the user ID number,);
the default wordpress posts display in the loop.
now the question is why custom post posts dont show up? or where does the information of author ID is stored for custom posts if at all.
BTW tried using autor_name as well with no success.
Thanks,
David
Hi Beda,
after further investigation I solved this issue and my code is working.
Thanks,
David