CustomPress ordering by custom field using datepicker

Hey, I'm trying to figure out how to display a list of PDFs by date. I currently added a date picker as a custom field in CustomPress to grab the date. The client wishes the list to display by most recent date to most past date. For instance, they want a July 2015 to show up before a June 2015 and May 2015, etc.

<ul>
					<?php
                        $args = array(
							'post_type' => 'attachment',
							'posts_per_page' => -1,
							'order'     => 'DESC',
							'meta_key' => 'ct_Image_Date_datepicker_e805',
							'orderby'   => 'meta_value',
							'meta_query' => array(
										array('key' => 'ct_Image_Date_datepicker_e805',
											  'value' => date( 'M j, y' ),
											  'compare' => '<'
										)
									),
							'category' => get_cat_id( single_cat_title("",false) )
                        );
						$attachments = get_posts($args);
						foreach ($attachments as $post): setup_postdata($post);
							$ID = get_the_ID();
							$page_data = get_post($ID, ARRAY_A);
							$content = $page_data['post_content'];
							$title = $page_data['post_title'];
							$file = $page_data['guid'];
							#echo $title . '  ' . $content;
							#echo '<p>&nbsp;<hr />&nbsp;</p>';
							#echo '<img src="img/pdf-icon.png">' . the_attachment_link($post->ID, false) . ' | ' . $title . '<br />';
							echo '<img src="' . get_template_directory_uri() . '/img/pdf-icon.png" style="margin-right:5px;">' . '<a href="' . $file . '">' . $title . '</a>' . '<br />';

							#. ' | ' . do_shortcode('[ct id="ct_Image_Date_datepicker_e805" property="title | description | value"]') .
						endforeach;
						wp_reset_postdata();
                    ?>
                    </ul>

I have no idea what I'm doing wrong. I've looked at other past date picker support tickets and I've tried those queries.. unsuccessful.

Thanks!

  • goerie
    • WPMU DEV Initiate

    Okay, the query currently works the way it should, however, I need it to sort by year first, not month/day. You would think that by adding the date picker field, that it would understand if I simply sorted by DESC or ASC, that it would automatically recognize the format of the date field.

  • goerie
    • WPMU DEV Initiate

    Hm actually yes, is there a way to split it by year? For instance, when the year changes from 2014 to 2015 or 2015 to 2016, have a horizontal line appear? I think the only way of doing that is through multiple queries?

  • Vaughan
    • Support/SLS MockingJay

    Hi,

    I'm not sure, I think yes, you would need to do multiple queries for each year, I don't think there's anything you can do with conditions in the above code, however I have asked one of our SLS devs for some advice on this for you as they are more experienced with code than I am.

    He should respond shortly, however it could take a little longer than usual depending on his workload.

    Thanks

  • Ash
    • WordPress Hacker

    Hello @goerie

    I hope you are well today.

    You may try the following way:

    $x = date( 'Y' );
    foreach ($attachments as $post): setup_postdata($post);
    	$ID = get_the_ID();
    	$page_data = get_post($ID, ARRAY_A);
    	$content = $page_data['post_content'];
    	$title = $page_data['post_title'];
    	$file = $page_data['guid'];
    	echo '<img src="' . get_template_directory_uri() . '/img/pdf-icon.png" style="margin-right:5px;">' . '<a href="' . $file . '">' . $title . '</a>' . '<br />';
    	$meta = get_post_meta( $ID, 'ct_Image_Date_datepicker_e805', true );
             // We should get the year of the meta data - datepicker
    	$year = date( 'Y', strtotime( $meta ) );
    	if( $year != $x ){
    		echo '<hr />';
    		$x = $year;
    	}
    endforeach;

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.