Skip to main content

Advanced Custom Fields is one of my favorite WordPress plugins.  It has so many different uses and really cuts down on the amount of custom code you need to write.

In this example you’ll see how to order the results of your query when outputting them to the front end page.

The field name in this example is “brand_name”

<?php 

// get posts
$posts = get_posts(array(
	'post_type'			=> 'event',
	'posts_per_page'	=> -1,
	'meta_key'			=> 'brand_name',
	'orderby'			=> 'meta_value',
	'order'				=> 'DESC'
));

if( $posts ): ?>
	
	<ul>
		
	<?php foreach( $posts as $post ): 
		
		setup_postdata( $post )
		
		?>
		<li>
			<a href="<?php the_permalink(); ?>"><?php the_title(); ?> (date: <?php the_field('brand_name'); ?>)</a>
		</li>
	
	<?php endforeach; ?>
	
	</ul>
	
	<?php wp_reset_postdata(); ?>

<?php endif; ?>

Leave a Reply