Get CPT title for hidden custom form field in frontend subscription form
Support › MB Frontend Submission › Get CPT title for hidden custom form field in frontend subscription formResolved
- This topic has 9 replies, 2 voices, and was last updated 2 years, 2 months ago by
Codog.
-
AuthorPosts
-
January 21, 2023 at 3:06 AM #40274
Codog
ParticipantHi there,
I have a front end 'subscription' form created using AIO custom fields and an associated 'subscription' CPT. This form is embedded via a shortcode on a 'Single CPT (projects) template'. All works fine.In this front end 'subscription' form I have a hidden custom field in which I am trying to extract the value of 'the_title()' from the individual 'Single CPT (projects) template' (Where the subscription form resides) - so it can be used in 'std' => 'value' within this hidden field.
Q) I know I can easily populate this hidden field with 'the_title()' value from each post using some javascript, but it somehow does not feel the "right" way to do it? Feels more robust to do it with some PHP, perhaps using Metabox's built in 'query_args' => [] to poulate this hidden field? Or can you think of another way this might be achieved using metabox AIO? Basically, I want to get the 'the_title()' value from each 'Single CPT (projects)' page where the 'subscription' form is present.
Any advice appreciated 🙂
January 21, 2023 at 9:01 PM #40278Peter
ModeratorHello there,
So how the title of the project is processed? Do you want to add this title as a custom field to the subscription post created after submitting the form? Or create a relationship between the current project post and the subscription post created?
January 21, 2023 at 11:32 PM #40281Codog
ParticipantHi Peter,
thanks for getting back to me.To answer you questions....
Q1) So how the title of the project is processed?
A2)1 It is user generated by the "Projects" CPT in a custom field that uses 'post_title'. Output to the single projects page temple is via the_title().
Q2) Do you want to add this title as a custom field to the subscription post created after submitting the form? Or create a relationship between the current project post and the subscription post created?
A2) I am open to either method as long as it works "Dynamically"?
First Suggested Method:
Yes, but NOT add it "Manually" within the wp-admin. This is why (As previously explained) I thought using a hidden field within the 'Subscription form' to capture the 'Project Post Title' was an appropriate method?Second Suggested Method:
Yes, this could work. However, I have already tried using the 'MB Relationships extension' and it works well for "Manually" selecting the 'project' post title in the Metabox provided inside the 'Subscription CPT' form in the wp-admin. However, I want to do this "dynamically" - I.E I would like the relationship to be done automatically, because I need to know which 'Project Post' the end user subscribed from using the 'Subscription form' embedded within it.Would this be possible using MB_Relationships_API? If yes, could you give me a starting point example? It would be very useful.
Hope this is clear?
Any help appreciated 🙂
January 23, 2023 at 10:03 PM #40290Peter
ModeratorHello,
Thanks, I understand your case. So if you want to add the relationship automatically, please refer to this topic https://support.metabox.io/topic/connecting-child-posts-to-a-parent-post-automatically/#post-35799
and the documentation https://docs.metabox.io/extensions/mb-relationships/#addLet me know how it goes.
January 23, 2023 at 10:08 PM #40292Codog
ParticipantThanks Peter!
I shall take a look at those and let you know how I get on.Thanks for your time 🙂
January 24, 2023 at 7:16 PM #40298Codog
ParticipantHi peter,
well I have had a look at those articles relating to my issue and I'm not having any success?Here is my current code...
1) Register the relationship (Between 'Subscribers' and 'Projects'):
add_action( 'mb_relationships_init', 'subscriber_project_relationship' ); function subscriber_project_relationship() { MB_Relationships_API::register( [ 'id' => 'subscriber-to-project', 'reciprocal' => true, 'from' => [ 'object_type' => 'post', 'post_type' => 'project_subscriber', 'empty_message' => 'No connection here', 'admin_column' => true, ], 'to' => [ 'object_type' => 'post', 'post_type' => 'user_project', 'empty_message' => 'No connection here', 'admin_column' => true, ], ] ); }
2) Get the project (Where the subscription form was submitted):
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) { MB_Relationships_API::add( get_queried_object_id(), $post_id, 'subscriber-to-project', $order_from = 1, $order_to = 1 ); }, 10, 2 );
3) The expectation:
The "Connects To" admin column within the "Subscriber CPT form Entry" displays the "Project Title" (Where the subscriber form was submitted).
4) What I actually get:
a) Empty "Connects To" admin column. (Not even the 'empty_message' => 'No connection here', which I assume should display when there is no connection)?
b) Duplicate "Subscriber CPT form Entries"?What am I missing / not understanding?
Many Thanks in advance.
January 28, 2023 at 10:08 AM #40329Peter
ModeratorHello,
The function
get_queried_object_id()
will return the current post where the form is located. It would be the project ID.
The variable$post_id
is thesubscription
post ID which will be created after submitting the post.So you can try to switch them and see if it works
MB_Relationships_API::add( $post_id, get_queried_object_id(), 'subscriber-to-project', $order_from = 1, $order_to = 1 );
January 29, 2023 at 11:33 PM #40347Codog
ParticipantHi Peter,
tried the above. It returns nothing? No relationship is generated? Just empty admin columns?Any ideas on what might be missing here?
Cheers 🙂
January 30, 2023 at 9:17 PM #40353Peter
ModeratorHello,
Please share your site credentials via the following contact form https://metabox.io/contact/. I will help you to investigate the issue.
February 3, 2023 at 7:02 PM #40390Codog
ParticipantHi Peter,
thanks for getting back to me and offering to help. Not to worry, I have now solved my issue.Cheers 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.