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

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #40274
    CodogCodog
    Participant

    Hi 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 🙂

    #40278
    PeterPeter
    Moderator

    Hello 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?

    #40281
    CodogCodog
    Participant

    Hi 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 🙂

    #40290
    PeterPeter
    Moderator

    Hello,

    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/#add

    Let me know how it goes.

    #40292
    CodogCodog
    Participant

    Thanks Peter!
    I shall take a look at those and let you know how I get on.

    Thanks for your time 🙂

    #40298
    CodogCodog
    Participant

    Hi 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.

    #40329
    PeterPeter
    Moderator

    Hello,

    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 the subscription 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 );
    
    #40347
    CodogCodog
    Participant

    Hi Peter,
    tried the above. It returns nothing? No relationship is generated? Just empty admin columns?

    Any ideas on what might be missing here?

    Cheers 🙂

    #40353
    PeterPeter
    Moderator

    Hello,

    Please share your site credentials via the following contact form https://metabox.io/contact/. I will help you to investigate the issue.

    #40390
    CodogCodog
    Participant

    Hi Peter,
    thanks for getting back to me and offering to help. Not to worry, I have now solved my issue.

    Cheers 🙂

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.