Support

Account

Home Forums General Issues Order by date loop problem

Solving

Order by date loop problem

  • I am following instructions in documentation on how to order posts by the custom date field – and I’ve stuck. Mainly on the loop part,
    here is the code from the tutorial:

    <?php
     
    /*
    *  Create PHP DateTime object from Date Piker Value
    *  this example expects the value to be saved in the format: yymmdd (JS) = Ymd (PHP)
    */
     
    $date = DateTime::createFromFormat('Ymd', get_field('date_picker'));
    echo $date->format('d-m-Y');
     
    /*
    *  Order Posts based on Date Picker value
    *  this example expects the value to be saved in the format: yymmdd (JS) = Ymd (PHP)
    */
     
    $posts = get_posts(array(
    	'meta_key' => 'custom_order', // name of custom field
    	'orderby' => 'meta_value_num',
    	'order' => 'ASC'
    ));
     
    if( $posts )
    {
    	foreach( $posts as $post )
    	{
    		setup_postdata( $post );
     
    		// ...
     
    	}
     
    	wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
    } ?>

    I am not quite sure where do I put the code mentioned there, do I put it instead my loop, or inside it? I particularly stuck on this part of the code from the help, as it looks completely different then my loop:

    if( $posts )
    {
    	foreach( $posts as $post )
    	{
    		setup_postdata( $post );
     
    		// ...
     
    	}

    If I use ordinary WP loop then it works, but I can’t use the date part above it, cause then it displays nothing

    Also another problem is I would like to display only posts from one category, and it doesn’t work, here is what I have:

    <?php
     
    /*
    *  Order Posts based on Date Picker value
    *  this example expects the value to be saved in the format: yymmdd (JS) = Ymd (PHP)
    */
     
    $posts = get_posts(array(
    	'meta_key' => 'event_date', // name of custom field
    	'orderby' => 'meta_value_num',
    	'order' => 'ASC'
    ));?>
    
    	<?php if (query_posts($posts . 'cat=23')) : while (have_posts()) : the_post(); ?>
    		
    		<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    			<div class="entry">
    				<?php the_post_thumbnail( 'whatson-thumb' ); ?>
    				<h2 class="pagetitle"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
    				<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
    				
    			</div>
    
    		</div>
    
    	<?php endwhile; ?>
    	<?php wp_reset_postdata(); ?>
    
    	<?php else : ?>
    
    		<h2>Not Found</h2>
    
    	<?php endif; ?>

    Thanks in advance!

  • You could use WP_Query for your basic loop (sorted by custom field ‘start_date’) like this:

    $args = array(
    	'post_type'		=> 'your_custom_post_type',
    	'posts_per_page'	=> -1,
    	'meta_key'		=> 'start_date',
    	'orderby'		=> 'meta_value_num',
    	'order'			=> 'DESC'
    );
     
    $wp_query = new WP_Query( $args );
    
    while( $wp_query->have_posts() )
    {
    	$wp_query->the_post();
    	// now use get_field() for custom fields
    }
  • If you want to only display posts from a certain category id (or array of category ids), it’s just one extra line in the WP_Query arguments, like this:

    $query = new WP_Query( 'cat=2,6,17,38' );

    If you don’t want to use ID’s, you can do this:

    $query = new WP_Query('category_name=staff');

    You can of course combine this with the sorting by date from above – this is just one of the arguments.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.