WordPress.org

Ready to get started?Download WordPress

Forums

Conditions in wordpress loop (4 posts)

  1. jhonb
    Member
    Posted 1 year ago #

    Hi guys,

    I have a little problem using the wordpress loop. I need to display events so I request 5 post_per_page events and hide passed events. My probleme is when i use the loop, wordpress take 5 posts from the query and hide the passed events. Is there a way to query ONLY the coming events?

    Here is my code!

    <?php
    	$args = array(
    	'post_type' => 'events',
    	'orderby'   => 'event_date',
    	'meta_key'  => 'event_date',
    	'order'     => 'ASC',
    	'posts_per_page' => '5',
    	);
    
    	query_posts( $args );
    	if ( have_posts() ) {
    	?>
    
    	<ul class="list_elements">
    		<?php
    		while ( have_posts() ) {
    		the_post();
    		$event_date = get_post_meta($post->ID, 'event_date', true);
    		$location = get_post_meta($post->ID, 'event_location', true);
    
    		if (date("d/m/Y", $event_date) >= date("d/m/Y") ) {
    			global $post;
    		?>
    			<li class="clearfix"><a>">
    				<?php the_post_thumbnail("partners"); ?>
    				<h3><?php the_title(); ?></h3>
    				<p><?php echo date("d/m/Y", $event_date); ?> -  <?php echo $location; ?></p>
    
    				<img class="facebook_icon" src="<?php bloginfo("stylesheet_directory"); ?>/public/img/facebook_icon.png" alt="facebook_icon" width=16"" height="16" />
    			</a></li>
    
    			<?php
    		}
    	  }
     echo '</ul>';
    }
     wp_reset_query();
    ?>

    Thanks for the help guys ! :)

    [No bumping. If it's that urgent, consider hiring someone.]

  2. vtxyzzy
    Member
    Posted 1 year ago #

    First, note that you cannot compare dates in 'd/m/Y' format. Dates must be in 'Y-m-d' format to compare properly. Once you have your Custom Fields in this format, try changing your code to this:

    <?php
    $today = date('Y-m-d');
    $args = array(
       'post_type' => 'events',
       'orderby'   => 'meta_value',
       'meta_key'  => 'event_date',
       'meta_value' => $today,
       'meta_compare' => '>=',
       'order'     => 'ASC',
       'posts_per_page' => '5',
    );
    
    query_posts( $args );
    if ( have_posts() ) {
       ?>
    
       <ul class="list_elements">
          <?php
          while ( have_posts() ) {
             the_post();
             $event_date = get_post_meta($post->ID, 'event_date', true);
             $location = get_post_meta($post->ID, 'event_location', true);
    
             global $post;
             ?>
             <li class="clearfix"><a>">
                <?php the_post_thumbnail("partners"); ?>
                <h3><?php the_title(); ?></h3>
                <p><?php echo date("d/m/Y", strtotime($event_date)); ?> -  <?php echo $location; ?></p>
    
                <img class="facebook_icon" src="<?php bloginfo("stylesheet_directory"); ?>/public/img/facebook_icon.png" alt="facebook_icon" width=16"" height="16" />
             </a></li>
    
             <?php
          }
       echo '</ul>';
    }
     wp_reset_query();
    ?>
  3. jhonb
    Member
    Posted 1 year ago #

    Hey @vtxyzzy,

    Thanks for the reply ! I tried:

    <?php
    $event_date = get_post_meta($post->ID, 'event_date', true);
    $date = date('Y-m-d', $event_date);
    $today = date('Y-m-d');
    
    $args = array(
         'post_type' => 'events',
         'orderby'   => 'meta_value',
         'meta_key'  => 'event_date',
         'meta_value' => $today,
         'meta_compare' => '>=',
         'order'     => 'ASC',
         'posts_per_page' => '5',
     );
    
    query_posts( $args );
     if ( have_posts() ) {
    ?>

    Doesn't work, any idea? Thanks !

    I think $event_date = get_post_meta($post->ID, 'event_date', true); doesn't return anything outside the loop :(

  4. jhonb
    Member
    Posted 1 year ago #

    Tried this too

    <?php
    $event_date = get_post_meta($post->ID, 'event_date', true);
    
    $args = array(
         'post_type' => 'events',
         'orderby'   => 'meta_value',
         'meta_key'  => date('Y-m-d', $event_date),
         'meta_value' => $today,
         'meta_compare' => '>=',
         'order'     => 'ASC',
         'posts_per_page' => '5',
      );
    
    query_posts( $args );
    if ( have_posts() ) {
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic