Home Forums Front-end Issues Sort Custom Post Type entries by 2 ACF select fields


Sort Custom Post Type entries by 2 ACF select fields

  • 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’ve created using the Advanced Custom Fields Select menu option. 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 an ACF 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 an ACF 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:

    • Spring 2012
    • Summer 2012
    • Autumn 2012
    • Winter 2012


    • Spring 2011
    • Summer 2011
    • Autumn 2011
    • Winter 2011

    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.

    $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(
    				'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 '';
    		echo $pdf_file->description;
    		echo '
    	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>';
    	$query = null;

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

  • Hi @unwrittendevin

    Thanks for the detailed question and code.

    I guess the only thing I don’t understand is what does work with your code?

    Does google have much in the way of WP and multiple meta field sorting?

    Maybe you will need to use one of WP’s query filters on the SQL to customize the ORDERBY

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

The topic ‘Sort Custom Post Type entries by 2 ACF select fields’ is closed to new replies.