Created
February 5, 2014 21:25
-
-
Save Shelob9/8833452 to your computer and use it in GitHub Desktop.
Reorder a WordPress custom post type archive page by the value of a meta field. Based on this SE answer: http://wordpress.stackexchange.com/a/48658/25300
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function slug_order_archive_by_field( $query ) { | |
//Only do if is main query fo a specific cpt's archive. | |
//@TODO set cpt's name | |
if ( $query->is_main_query() && is_post_type_archive( 'CPT_NAME' ) ) { | |
//@TODO Set the name of the field we are ordering by | |
$query->set( 'meta_key', 'FIELD_NAME' ); | |
//@TODO change order to DESC IF you want to go in reverse order | |
$query->set( 'order', 'ASC' ); | |
//@TODO uncomment next line IF you want to sort by an integer instead of alphabetically | |
//$query->set( 'orderby', 'meta_value_num' ); | |
} | |
} | |
add_action( 'pre_get_posts', 'slug_order_archive_by_field' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment