0

I have a custom post type called "Events". The post displays fine, except that the WP Query loop only displays one post instead of all of them. Here is the complete code from my functions.php file for Events:

    function min_get_events ( $echo = true ) {
     $events = new WP_query(array(
    'post_type' => 'events',
    'posts_per_page' => -1,
    'order' => 'ASC',
    'orderby' => 'meta_value',
    'meta_key' => 'min_event-start',
    'meta_query' => array(
        'key'     => 'min_event-start',
        'value'   => date('Y-m-d'),
        'compare' => '>',
    )
));

$i = 0;
$n = 1;

if ( $events->have_posts() ) :
    while ( $events->have_posts() ):
        global $post;
        $events->the_post();

        $open_date = get_post_meta($post->ID, 'min_event-start', true);
        $close_date = get_post_meta($post->ID, 'min_event-end', true);
        $start = strtotime($open_date);
        $end = strtotime($close_date);
        if ($start==$end || $start > $end) {
            $date_string = date('F j, Y', $start);
        }
        else {
            $date_string = date('F j, Y', $start) .' – '. date('F j, Y', $end);
        }
        //$description = get_post_meta($post->ID, 'event-description', true);
        $location = get_post_meta($post->ID, 'min_event-location', true);
        $url = get_post_meta($post->ID, 'min_event-url', true);
        //$registration_url = get_post_meta($post->ID, 'event-registration-url', true);
        $cta_text = get_post_meta($post->ID, 'min_event-cta-text', true);
        $cta = !empty($cta_text) ? $cta_text : 'Register Now';

        $i++;
        if ( $i == 1 ) {
            ?>
            <div class="row">
            <?php
        }
        ?>
        <div class="event col-sm-4">
            <a href="<?= $url ?>">
                <?php the_post_thumbnail('thumb-events'); ?>
            </a>
            <h1><?= htmlentities($post->post_title) ?></h1>
            <div >
                <div><?= $date_string; ?></div>
                <div><?= htmlentities($location) ?></div>
                <div><a href="<?=$url;?>" class="register-now"><?= $cta; ?></a></div>
            </div>
            <!-- <p><?= $description ?></p> -->
            <!-- <p><a href="<?=$url;?>" class="register-now"><?= $cta; ?></a></p> -->
        </div> <!-- .event -->
        <?php


        if ( $i == 3  || ($events->current_post +1) == $events->post_count) {
            ?>
            </div> <!-- <?php echo $i; ?> .row <?php echo $n; ?> -->
            <?php
            $i = 0;
            $n++;
        }

    endwhile;
endif;

ob_start();
?>

<?php
$return = ob_get_contents();
ob_end_clean();
wp_reset_postdata();
if ( $echo ) {
    echo $return;
} else {
    return $return;
}
 }

Am I missing something here?

8
  • Are you sure the query returns more than one post?
    – cjbj
    Nov 15, 2016 at 19:37
  • @cjbj it's only returning 1 post. There's currently 9 posts for that post type
    – MikeL5799
    Nov 15, 2016 at 19:41
  • Do you get 4 results if you set posts_per_page param to 4? The purpose is to determine if that param is affecting your query at all. A pre_get_posts hook somewhere might be limiting your result as well?
    – jdm2112
    Nov 15, 2016 at 19:45
  • 1
    @jdm2112 just checked for pre_get_posts not in the code anywhere
    – MikeL5799
    Nov 15, 2016 at 19:51
  • 1
    In that posttype. perhaps. But you're also checking against a date.
    – cjbj
    Nov 15, 2016 at 20:00

1 Answer 1

1

The meta_query parameter must be change in an array of array for a single custom field handling:

 'meta_query' => array(
     array(
         'key'     => 'min_event-start',
         'value'   => date('Y-m-d'),
         'compare' => '>',
    )
 )

You will the informations WP_Query in the Single Custom field handling part

Hope it will work with this.

1
  • 1
    Good catch. Didn't notice this. Meta query is an array of arrays.
    – jdm2112
    Nov 15, 2016 at 22:22

Your Answer

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

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