Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created August 28, 2012 20:16
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chrisguitarguy/3503671 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/3503671 to your computer and use it in GitHub Desktop.
Force orderby to `menu_order` on a custom post type archive and a few custom taxonomy archives
<?php
add_action('parse_query', 'pmg_ex_sort_posts');
/**
* Hooked into `parse_query` this changes the orderby and order arguments of
* the query, forcing the post order on post type archives for `your_custom_pt`
* and a few taxonomies to follow the menu order.
*
* @param object $q The WP_Query object. This is passed by reference, you
* don't have to return anything.
* @return null
*/
function pmg_ex_sort_posts($q)
{
if(!$q->is_main_query() || is_admin())
return;
if(
!is_post_type_archive('your_custom_pt') &&
!is_tax(array('some_taxonomy', 'assigned_to_your_pt'))
) return;
$q->set('orderby', 'menu_order');
$q->set('order', 'ASC');
}
@reneegade
Copy link

Works beautifully - thank you.

@marbaque
Copy link

doesn't work for me I have tried everything, my custom post type is always ordered by date.

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