For a project of mine I have set up three custom fields (Year, month, day) attached to posts in order to determine the original date of a post’s content (not the post publish date, which is used for ordering in a different template). For an archive/category view I have successfully arranged to have them ordered by year (custom field: date_year), then month (date_month), then day(date_day) via the following query arguments:
'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_query' => array( array( 'key' => 'date_year', 'value' => '', 'compare' => 'LIKE' ), array( 'key' => 'date_month', 'value' => '', 'compare' => 'LIKE' ), array( 'key' => 'date_day', 'value' => '', 'compare' => 'LIKE' ) )
However, the project would require to separate each year’s posts into a separate div, each with a unique ID, e.g.
<div id="year-2015"> <!-- All posts from 2015, ordered by month and then day --> </div> <div id="year-2014"> ... </div>
Is there an easy way to do so? How would I have to query in order to achieve this? Or would you in general (even though the project requires three separate fields) suggest one field and then explode it to retrieve the year for separating into divs?
One single date field (format Y-m-d: e.g. "2015-02-18") would be even better, as it turns out. A basic query would be
$args = array( 'cat' => 14, 'post_type' => 'post', 'posts_per_page' => -1, 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => 'p2f_date' );
in order to have normal order via date field as intended, but of course there is no grouping by year so far.