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.
is_post_type_archive( 'simple_class' )
check will return false in the admin. So, some other code/plugin may be affecting the admin area.meta_value
. Is iton
/off
. Is it1
/0
?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.