Custom Taxonomy order by Custom Field

Hope somebody can help. I have a custom post type called “dinner-dates”, which has a custom field called “show-date” which displays the dates of dinner-date events.

The custom post type has a custom taxonomy called “Show-Name” which outputs various shows by show name IE: “Fawlty Towers” etc.

When I click a particular show name on my nav menu, it displays a grid of all upcoming events called “Fawlty Towers” and displays them.

What I would like to do now is order these events by there show date. So order by meta-value-num ASC.

How do I do this on a taxonomy page? At the moment it is using the standard..

while have posts loop.

When I tried doing a custom query the output on the taxonomy template page is to display ALL show events in the correct order. However I only want to display the Fawlty Towers ones not every possible Show.

Hope that makes sense.

I am guessing you have to order it by whatever the current taxonomy term is, but I am not sure how I go about grabbing the info in this way and ordering it correctly.

I will put the code in below, hopefully somebody can help…

Thanks Dan

<ul class="events-listing-index">

<?php: while (have_posts()) : the_post(); ?>

<li>
  //This is where the info about the show is, thumbnail, link to book tickets, more info about the show, the show date, venue and location.
</li>

<?php endwhile; ?>
</ul>

This is the code I run when I am on other pages throughout the site in order to order the posts by custom field.

$args = array(
    'post_type'         => 'cdc-dinner-dates',
    'posts_per_page'    => -1,
    'meta_key'          => 'wpcf-rtd-show-date',
        'orderby'           => 'meta_value_num',
        'order'             => 'ASC'
);
$query = new WP_Query( $args );
?>

This is how I want to order the posts, but on a taxonomy.php page.

1 Answer
1

Ok so after posting the question an option to view another similar question appeared. So here is the answer if somebody is trying to do the same thing.

//Using WordPress Pre-Get filter to order the custom taxonomy by custom field
function customize_customtaxonomy_archive_display ( $query ) {
    if (($query->is_main_query()) && (is_tax('custom-taxonomy')))

    $query->set( 'post_type', 'your-post-type' );                 
    $query->set( 'posts_per_page', '-1' );
    $query->set( 'meta_key', 'your-custom-field' );           
    $query->set( 'orderby', 'meta_value_num' );
    $query->set( 'order', 'ASC' );
}

 add_action( 'pre_get_posts', 'customize_customtaxonomy_archive_display' );

Add this to your functions.php file, or create a plugin for it. Replace “custom-taxonomy”, “your-post-type” and “your-custom-field” with your details and you should get the results you are after.

Dan

Leave a Comment