• Resolved andrew-s3

    (@andrew-s3)


    Okay I’ve created the following loop in order to sort my posts from Category 2 by the user-entered date field ‘prana_event_date’ and display only the 5 next posts. I’m not totally sure I set this up properly as it’s not changing when I switch from ASC to DESC. Can an of you experts shine some light on my noobishness.

    <!-- LOOP START -->
    <?php //WP_Query Arguments
    	$args = array (
    		'cat' => '2',
    		'posts_per_page' => '5',
    		'meta_key' => 'prana_event_date',
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC',
    ); ?>
    <?php $the_query = new WP_Query( $args ); ?>
        <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
        <section><a href="<?php the_permalink() ?>"><?php the_post_thumbnail( array(175,110) );?><div class="overlay"></div></a><div class="event-info"><h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3><p class="date"><?php the_field( 'prana_event_date' ); ?></p><?php the_excerpt(); ?><a href="<?php the_permalink() ?>" class="more-info">more info ></a></div></section>
        <?php endwhile;?>
    <!-- LOOP FINISH -->
Viewing 8 replies - 1 through 8 (of 8 total)
  • How is the event date stored in the database? Is it a Unix time stamp?

    Thread Starter andrew-s3

    (@andrew-s3)

    I’m using the ‘Advanced Custom Fields’ Plugin to create my custom fields – here’s the settings for that field.

    Save Format – DD, MM dd
    Display Format – mm/dd/yy

    now the SAVE FORMAT is the one that gets entered into the database – if i need to switch this to a number string like ‘20130925’ how would I then convert that # into a String like – ‘Wednesday, September 25, 2013’ in my SECTION code?

    If your’e saving ‘20130925’, you could do the following:

    <?php
    while(have_posts()) : the_post();
      // get the date
      $date= get_field('prana_event_date');
      // convert to Unix timestamp, then reformat, then print result
      echo date(strtotime('M d Y', $date));
      // above prints ex. Sept 25 2013
    endwhile;
    ?>

    Thread Starter andrew-s3

    (@andrew-s3)

    now is there a way for me to convert that same data to “Wednesday, September 25, 2013”?

    Thread Starter andrew-s3

    (@andrew-s3)

    I’m sorry I completely spaced – I’m going to see if I can work with this and get it sorting properly by the 20130925 # – let’s see

    Thread Starter andrew-s3

    (@andrew-s3)

    Okay – I’ve implemented what you’ve suggested into my loop below – the problem seems to be that it’s not “printing” at all – nor are the posts being sorted ASC/DESC by my meta_key value.

    <!-- LOOP START -->
    <?php //WP_Query Arguments
    	$args = array (
    		'cat' => '2',
    		'posts_per_page' => '5',
    		'meta_key' => 'prana_event_date',
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC',
    		);
    		$date= get_field('prana_event_date');
    		?>
    <?php $the_query = new WP_Query( $args ); ?>
        <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
        <section>
        <a href="<?php the_permalink() ?>"><?php the_post_thumbnail( array(175,110) );?><div class="overlay"></div></a>
        <div class="event-info">
        <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
        <p class="date">
        	<?php
    			// convert to Unix timestamp, then reformat, then print result
    			echo date(strtotime('M d Y', $date));
    			// above prints ex. Sept 25 2013
    		?>
        </p>
    	<?php the_excerpt(); ?><p><?php the_field( 'prana_event_date' ); ?></p>
        <a href="<?php the_permalink() ?>" class="more-info">more info ></a>
        </div>
        </section>
        <?php endwhile;?>
    <!-- LOOP FINISH -->
    Thread Starter andrew-s3

    (@andrew-s3)

    I’ve modified my LOOP to the following:

    <!-- LOOP START -->
    <?php //WP_Query Arguments
    	$args = array (
    		'cat' => '2',
    		'posts_per_page' => '5',
    		'meta_key' => 'prana_event_date',
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC',
    		);
    		$date= get_field('prana_event_date');
    ?>
    <?php $the_query = new WP_Query( $args ); ?>
        <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
        <section>
        <a href="<?php the_permalink() ?>"><?php the_post_thumbnail( array(175,110) );?><div class="overlay"></div></a>
        <div class="event-info">
        <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
        <p class="date"><?php echo date(strtotime('M d Y', $date));	?></p>
    	<?php the_excerpt(); ?><p><?php the_field( 'prana_event_date' ); ?></p>
        <a href="<?php the_permalink() ?>" class="more-info">more info ></a>
        </div>
        </section>
        <?php endwhile;?>
    <!-- LOOP FINISH -->

    I’m still not getting a proper print of the date at all; it’s also not ordering by the meta_key prana_event_date either.

    Thread Starter andrew-s3

    (@andrew-s3)

    VICTORY IS MINE!

    <!-- LOOP START -->
    <?php //WP_Query Arguments
    	$args = array (
    		//'cat' => '2',
    		'posts_per_page' => '5',
    		'meta_key' => 'prana_event_date',
    		'meta_value' => date('Ymd'),
    		'meta_compare' => '>=',
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC',
    		);
    
    ?>
    <?php $the_query = new WP_Query( $args ); ?>
        <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
        <section>
    		<?php $date= get_field('prana_event_date'); ?>
        <a href="<?php the_permalink() ?>"><?php the_post_thumbnail( array(175,110) );?><div class="overlay"></div></a>
        <div class="event-info">
        <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
        <p class="date"><?php echo date('l, F jS, Y', strtotime($date)); ?></p>
    	<?php the_excerpt(); ?>
        <a href="<?php the_permalink() ?>" class="more-info">more info ></a>
        </div>
        </section>
        <?php endwhile;?>
    <!-- LOOP FINISH -->
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[LOOP] Trouble w/ orderby metakey values’ is closed to new replies.