mercredi 2 décembre 2015

Wordpress PHP echo result with query

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?

Aucun commentaire:

Enregistrer un commentaire