Using a Custom Field (date) to sort posts

Hey All,

I am using the most up to date version of XPro (everything is updated and validated). I created a custom post type “events” and I have added a custom field to this post type called “Start Date”, which I have used the “date selector” within Custom Fields. How am I able to sort the list of posts by this custom field? Is it possible?

Any help would be appreciated.

Thanks!

Just to let anyone know who is wondering, I have figured it out by using this code:

function customize_customtaxonomy_archive_display ( $query ) {
    if (($query->is_main_query()) && (is_tax('custom-taxonomy-name'))) {
      $query->set( 'post_type', 'custom-post-type-name' );                 
      $query->set( 'posts_per_page', '-1' );
      $query->set( 'meta_key', 'custom-field-name' );           
      $query->set( 'orderby', 'meta_key' );
      $query->set( 'order', 'ASC' );
    }
}
 add_action( 'pre_get_posts', 'customize_customtaxonomy_archive_display' );

All you have to do is replace “custom-taxonomy-name”, “custom-post-type-name” and “custom-field-name” with your appropriate fields.

Thanks!

Hi @michigancreative,

Thanks for reaching out.

Just for future topics, self responding or bumping your post pushes it back in our Queue system so it takes longer to respond to.

On the other hand, we’re glad that you’re able to figure it out! If you have any other concerns or clarifications regarding our theme features, feel free to open up a new thread.

Cheers!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.