0

I am working on a WordPress plugin and trying to use the pre_get_posts hook. Everything seems to sort, but when I add new items through the back-end, they get added to the bottom and not sorted for some reason. Hoping a fresh set of eyes could help.

add_action( 'pre_get_posts', 'simple_class_custom_query' );

function simple_class_custom_query ( $query ) {
    // Sort Archive Page Results
    if ( is_post_type_archive( 'simple_class' ) && $query->is_main_query() ) {

        // Sort results by class day
        $query->set( 'meta_key', 'class-day' );

        // orderby class start
        $query->set( 'orderby', array( 'meta_value' => 'ASC', 'class-start' => 'ASC' ) );

        // Hide hidden classes
        $query->set( 'meta_query', array(
            array(
                'key' => 'hide-class',
                'value' => '1',
                'compare' => 'NOT EXISTS'
            )
        ));
    }
}

Thanks in advance! Should add this has a few pieces. The hide-class is a meta checkbox to hide the class and the classes should be sorted by the day of the week the class takes place and then by start time.

5
  • The code posted would not affect the back end since the is_post_type_archive( 'simple_class' ) check will return false in the admin. So, some other code/plugin may be affecting the admin area. Commented Nov 4, 2016 at 6:35
  • ..unless this is part of file that is loaded in admin only.
    – Z. Zlatev
    Commented Nov 4, 2016 at 15:45
  • Show us the meta_value. Is it on/off. Is it 1 / 0 ?
    – Z. Zlatev
    Commented Nov 4, 2016 at 15:47
  • Perhaps I should be using meta_value_num. It's a range from 1 to 7, corresponding to a day of the week. This is for the front end of the site and not the admin area. What's happening here (or at least should be) is to sort classes by date and then by start time. Commented Nov 10, 2016 at 18:52
  • Ok. That is_post_type_archive( 'simple_class' ) is the problem as I'm using this on a page through a shortcode. So none of the query items are actually being run. Commented Nov 10, 2016 at 19:04

0

Browse other questions tagged or ask your own question.