quasel
12/4/2017 - 10:23 AM

Force orderby to `menu_order` on a custom post type archive and a few custom taxonomy archives

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');
}