Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Created March 14, 2015 19:38
Show Gist options
  • Save Shelob9/5629ec07d636d985139e to your computer and use it in GitHub Desktop.
Save Shelob9/5629ec07d636d985139e to your computer and use it in GitHub Desktop.
Order a post type archive by a custom field
<?php
add_action( 'pre_get_posts', function ( $query ) {
if ( $query->is_main_query() && !is_admin() && $query->is_post_type_archive( 'name_of_post_type') ) {
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'custom_field_name');
$query->set('order', 'ASC');
}
}
@MykeyMaker
Copy link

What is meta_value_num?
How would I find it when using this:
https://gist.github.com/Shelob9/6679914
will these things work together?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment