0

I have a list of results on a page (each result is the title of a custom post type). Each custom post has meta data attached (stored in wp_postmeta in the db), one of the bits of meta data is the variable wedding_hidden_is_completed

For each of my custom posts where this variable is set to 0, I want to echo an error message.

For example, say I have posts with ID's 1,2,3,4 and only posts 2 and 3 have the variable wedding_hidden_is_completed set to 1, then I need to echo 2 separate error messages: Error with Post 1 AND Error with post 4 (these should be wrapped in separate divs).

Here is what I have so far:

global $wpdb;
$args = array(
'post_type' => 'bookings',
'meta_query' => array(
    array(
        'key' => 'wedding_user',
        'value' => $current_user->display_name,
        'compare' => '=',
    )
)
);
// Errors query
$errors_query = new WP_Query( $args );
// The Loop
if ( $errors_query->have_posts() ) {
while ( $errors_query->have_posts() ) {
    $errors_query->the_post();

// Fill out form \
$is_form_complete = get_post_meta( $errors_query->post->ID, 'wedding_hidden_is_completed', true );

if($is_form_complete !=1) {
$error_message = '<p>Error with post'.$errors_query->post->ID.'</p>';
}

?>
<div class="errors"><?php echo $error_message; ?></div>
<?php
}
}

This currently echos out 4 divs (one for each post type), the first 3 say error with post 1, and the last one says error with post 4. How can I make it so that I only echo a div per error?

UPDATE: code showing all error if statements

if($is_form_complete !=1) {
  $error_message = '<p>Booking form ID DISTR'.$errors_query->post->ID.' needs to be completed.</p>';
  echo '<div class="errors"><i class="fa fa-clock-o"></i>'.$error_message.'</div>';
}

// Deposit due
if($is_deposit_paid !=1) {
  $error_message = '<p>Booking form ID DISTR'.$errors_query->post->ID.' deposit now due.</p>';
  echo '<div class="errors"><i class="fa fa-clock-o"></i>'.$error_message.'</div>';
}

// Full Payment due
if($is_full_amt_paid !=1) {
  $error_message = '<p>Booking form ID DISTR'.$errors_query->post->ID.' remaining balance now due.</p>';
  echo '<div class="errors"><i class="fa fa-clock-o"></i>'.$error_message.'</div>';
}

1 Answer 1

2

Try this...

Replace

if($is_form_complete !=1) {
$error_message = '<p>Error with post'.$errors_query->post->ID.'</p>';
}

?>
<div class="errors"><?php echo $error_message; ?></div>
<?php

With

if($is_form_complete !=1) {
  $error_message = '<p>Error with post'.$errors_query->post->ID.'</p>';
  echo '<div class="errors">'.$error_message.'</div>';
}

That way you only output the error div when there is an error. This will stop it outputting the same error 3 times as per your example.

UPDATE:

For your multiple errors:

Before your loop

// Define the variable first and give it a default value
$error_message = null;

Then in your loop (using .= lets us add to the variable)

// Form not complete
if($is_form_complete !=1) {
  $error_message .= '<i class="fa fa-clock-o"></i><p>Booking form ID DISTR'.$errors_query->post->ID.' needs to be completed.</p>';
}

// Deposit due
if($is_deposit_paid !=1) {
  $error_message .= '<i class="fa fa-clock-o"></i><p>Booking form ID DISTR'.$errors_query->post->ID.' deposit now due.</p>';
}

// Full Payment due
if($is_full_amt_paid !=1) {
  $error_message .= '<i class="fa fa-clock-o"></i><p>Booking form ID DISTR'.$errors_query->post->ID.' remaining balance now due.</p>';
}

And finally after closing your loop

// Check if there are any errors to display
if($error_message != null) {
  echo '<div class="errors">'.$error_message.'</div>';
}
Sign up to request clarification or add additional context in comments.

4 Comments

Great stuff - the problem you had was that once you set $error_message it would stay set. So by keeping it all in the conditional (the if) then it only gets displayed when it should :)
Thanks Simon, I guess in the long run I would rather have all my messages stored as an array (as there will be more if statements) for example if($is_deposit_paid !=1) { - Ideally I'd like to just have one line of code that echo's out all the errors - do you think that is possible?
Yeah that should be possible - do you want each booking to have an error message or one big error message relating to all the bookings?
Yep the latter, so at the top of the page - above all the bookings is a list of all the errors relating to the bookings - does that make sense? Let me add all my error if statements to my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.