0

I am looking to associate an Application post type as a child of a Job post type, where users can submit an Application that is tied to the Job it was submitted for. The end goal is to allow the Author of a Job to view the Application posts submitted for that Job.

I have enabled hierarchies for both Post Types using CPT UI but cannot see any option to automatically set the Parent of the Proof post type to be the Job it was submitted for. I have also tried using the Advanced Custom Fields relationship field, but this only allows users to set the parent manually, rather than automatically associate the Proof with the parent Job.

Is it possible to associate a Custom Post Type with a parent Custom Post Type, and if so how?

Thank you very much.

0

Yes, it possible to set a post of one type to be a child of another type of post. You just need to make sure that the post_parent parameter is passed along other post data (post_title, etc.) when the post is created / inserted in to the database.

For example, if you have a custom form for submitting the applications, you could add the job post ID in a hidden field to it.

<form id="yourApplicationForm" method="post">

    <!-- form fields -->

    <input type="hidden" name="job_id" value="<?php echo esc_attr( $job_id ); ?>">

    <!-- submit button -->

</form>

When the form is submitted and the data is passed to your form handler, you'd grab the hidden field value from $_PSOT and set it as the post_parent value.

function your_form_submission_handler() {

    // code..

    // insert application post to database
    $application_post = wp_insert_post(
        array(
            // other_post_field_keys_and_values,
            'post_type' => 'application',
            'post_parent' => absint( $_POST['job_id'] ?? 0 ),
        )
    );

    // code..
    
}

WordPress will happily accept the job ID as post_parent even, if the ID is for a post of another type.


If, on the other hand, you're using some plugin or theme feature for the application form, then the idea stays the same, but the implementation might require a little more digging.

Basically, you would still add the job ID to the form as a hidden field, but you would need to find out how the form is saved, and how you can filter the form data to map the job ID as the post_parent for the application post.

If the form functionality also uses wp_insert_post in the end to save the submission as a post, you could then either use wp_insert_post_parent or wp_insert_post_data filters to set the post_parent from $_POST.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.