Tell us what you are trying to do?
Use get_posts to check if a post already exists with the same title that a form is attempting to create. The title is formed from a pair of select fields, so cannot be unpredictable.
Is there any documentation that you are following?
https://toolset.com/forums/topic/cred-before-save-data-allow-only-one-submition-of-the-form/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
https://codex.wordpress.org/Template_Tags/get_posts
Is there a similar example that we can see?
https://toolset.com/forums/topic/cred-before-save-data-allow-only-one-submition-of-the-form/
What is the link to your site?
Links and access can be provided in PM's.
All I have been trying to do is create a custom validation that assembles what will be saved as the post title (as stated above, it is formed from two select fields and as such is entirely predictable) and ensure that a post has not already been created with that title. This should have been a simple case of using get_posts to search for an existing post_title within the post_type being used. But no matter what arguments I pass to get_posts, it will always return an empty array! This happens if I try using the CPT, or just pages, or any other simple or complex set of arguments. Nothing is ever returned. I have even tried including the global posts argument, but that made no difference either.
Here is the relevant part of the snippet:
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
//Load list of field data comprised of field values and errors
list( $fields, $errors ) = $error_fields;
//uncomment this if you want to print the field values
//print_r($fields);
//** Validation for New Term form **//
if ( $form_data['id'] == 446 )
{
/*** Other working validation is here ***/
/* Check if a Term with the same name has already been created */
if ( isset( $fields['wpcf-school-term']['value'] ) )
{
global $posts;
//Assemble the name from the select fields
$current_term = $fields['wpcf-school-term']['value'] . " " . $fields['wpcf-term-year']['value'];
//Array of arguments to check existing academic-term posts
$args = array(
'orderby' => 'post_date',
'order' => 'ASC',
'posts_per_page' => 1,
'post_type' => 'pages',
'post_title' => $current_term
);
$terms = get_posts( $args );
if ( $terms['post_title'] == $current_term )
{
//set error message for post title
$errors['school-term'] = $fields['wpcf-school-term']['value'] . " " . $fields['wpcf-term-year']['value'] . ' already exists, so you cannot create another one.';
}
}
}
//Return array of results
return array($fields,$errors);
}
I also tried
if ( sizeof( $terms ) > 0 )
like the other post suggested, but it returns a size of 1, so always triggers the validation.
No matter what, the $terms array (and oddly the $args array if I try dumping it) always output an empty array.
The form is being loaded in a modal (I attached a screenshot for reference) and submitted via ajax, not that this should make any difference as all of the other validations work fine, so the hook is definitely firing properly. Also, before it is asked, 'academic-term' is definitely the correct post type, see second attached screenshot for proof. But as mentioned before, get_posts is refusing to return anything at all and I am stuck as to how I can debug it any further.
Thanks in advance.