Skip to content

Instantly share code, notes, and snippets.

@yaroslav-borodii
Last active May 23, 2023 19:34
Show Gist options
  • Save yaroslav-borodii/36ea35f1f9262edce95d41fa54a52612 to your computer and use it in GitHub Desktop.
Save yaroslav-borodii/36ea35f1f9262edce95d41fa54a52612 to your computer and use it in GitHub Desktop.
Retrieves a Ninja Forms submission by its form ID and sequence number.
<?php
/**
* Retrieves a Ninja Forms submission by its form ID and sequence number.
*
* @param int|string $form_id The ID of the form.
* @param int|string $seq The sequence number of the submission.
*
* @return NF_Database_Models_Submission Returns an NF_Database_Models_Submission from Ninja Forms
*/
if ( function_exists( 'Ninja_Forms' ) && ! function_exists( 'nf_get_sub_by_seq' ) ) {
function nf_get_sub_by_seq( $form_id, $seq ) {
global $wpdb;
$query = "
SELECT p.ID
FROM {$wpdb->posts} p
INNER JOIN {$wpdb->postmeta} pm1 ON p.ID = pm1.post_id
INNER JOIN {$wpdb->postmeta} pm2 ON p.ID = pm2.post_id
WHERE p.post_type = 'nf_sub'
AND pm1.meta_key = '_form_id' AND pm1.meta_value = %s
AND pm2.meta_key = '_seq_num' AND pm2.meta_value = %s
";
// Prepare and execute the SQL query
$sub_post_id = $wpdb->get_var( $wpdb->prepare( $query, $form_id, $seq ) );
// Retrieve the form submission using the post ID
return Ninja_Forms()->form( $form_id )->sub( $sub_post_id )->get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment