0

i only want to show upcoming events posts on the homepage. i have made a custom field using the 'custom field suite' plugin, called 'ENDS' representing the date an event ends.

as you can tell, i simply want upcoming events on the home, with the nearest to present date being first in line. upon expiring, i want it filed in the 'archive', which is visible on another page (which should also order archived event posts beginning with most recently expired event).

the template uses infinite scroll to load posts, and this is my index file, with the default loop: http://pastebin.com/x3NzZBZX

i need to integrate something like this, but have had no success:

<?php
$query = new WP_Query(
    'post_type' => 'post',
    'orderby' => 'meta_value',
    'meta_key' => 'ENDS',
    'order' => 'ASC',
    'posts_per_page' => -1,
    'meta_query' => array(
        'key' => 'ENDS',
        'value' => date('Y-m-d H:i:s'), // now
        'compare' => '>='
    ),
);
?>

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

<a href="<?php the_permalink(); ?>">
    <?php the_title(); ?>
</a>

<?php endwhile; ?>

1 Answer 1

2

I believe the meta_query part should be a nested array, so your query should be like this:

$query = new WP_Query(
    'post_type' => 'post',
    'orderby' => 'meta_value',
    'meta_key' => 'ENDS',
    'order' => 'ASC',
    'posts_per_page' => -1,
    'meta_query' => array(
        array (
            'key' => 'ENDS',
            'value' => date('Y-m-d H:i:s'), // now
            'compare' => '>=',
            'type' => 'DATETIME',
        ),
    ),
);

Note that I also added the 'type' => 'DATETIME' to the meta_query inner array.

Hope this helps.

1
  • thanks! i am also struggling in replacing the default loop in the index file. very limited php skills. Oct 3, 2012 at 19:39

Your Answer

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

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