• Hi,

    I’m trying to create a page to display publications by year and date. The issue I am running across is that I can sort by only one of my custom meta fields. I have a custom post type called “publications” to house all of the publication entries.

    My Meta Fields
    publication_year: Goes from 1998-2020, chosen from a select menu. I would like to sort chronologically downwards (2020 -> 1998)
    publication_date: Goes from Spring[1], Summer[2], Autumn[3], and Winter[4], chosen from a select field. I would like these to sort by the time of year.

    I have my posts organized by a custom taxonomy called “document”, so it groups each set of publications into one group. What I’d like to do is have each custom taxonomy then sort the posts by year, then organize them by time of year.

    For Example:

    <strong>Magazines</strong>
    <em>2012</em>
    
    <ul>
    <li>Spring 2012</li>
    <li>Summer 2012</li>
    <li>Autumn 2012</li>
    <li>Winter 2012</li>
    </ul>
    <em>2011</em>
    
    <ul>
    <li>Spring 2011</li>
    <li>Summer 2011</li>
    <li>Autumn 2011</li>
    <li>Winter 2011</li>
    </ul>

    and so on…

    I would also like to exclude any years that don’t have publications associated with them. Years 2014 through 2020 theoretically shouldn’t have anything yet, but I created them just for the future and my client not having to come back and ask me to add more select options. The year 1999 however does not have any publications though, so if there are no posts associated with that year, I want it excluded from the list. If my client does digitize their pubs from 1999, I want them to be able to add them and have the page populate with these posts.

    Here’s my code so far.

    <?php
    	$pdf_files = get_terms('document');
    	foreach ( $pdf_files as $pdf_file ) {
    	$query = new WP_Query(
    	$args = array(
    		'posts_per_page' => '-1',
    		'document' => $pdf_file->slug,
    		'post_type' => 'publication',
    		'meta_key' => 'publication_date',
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC',
    		'meta_query' => array(
    			array(
    				'key' => 'publication_year',
    				'orderby' => 'meta_value_num',
    			),
    		)
    	) );
    	echo '<h2 class="clearfix" id="'.$pdf_file->slug.'">';
    	echo $pdf_file->name;
    	echo '</h2>';
    	if ($pdf_file->description):
    		echo '<p>';
    		echo $pdf_file->description;
    		echo '</p>';
    	endif;
    	while ( $query->have_posts() ) : $query->the_post();
    		$my_year = get_field('publication_year');
    		echo get_field('publication_year');
    		echo '<div class="row-fluid pad-one-b clearfix">';
    		get_template_part('column', 'pubs');
    		echo '</div>';
    	endwhile;
    	wp_reset_postdata();
    	$query = null;
    	}
    ?>

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    Any thoughts as to how I would get it to sort and organize the posts correctly?

  • The topic ‘Sort Custom Post Type entries by 2 custom meta fields’ is closed to new replies.