latest posts-widget displays all posts on archive page but works great anywhere else

Thanks to the comments from @Pieter Goosen and @cybmeta I changed my way to order the posts on the archive page by making use of pre_get_posts

What I wanted my archive page to do is displaying posts with custom field value 1 and named “premium” (my method to create custom sticky posts) first and then all the “normal” posts.

I added the following lines to my functions.php file (Edit: added !is_admin() as suggested in Pieter’s comment):

function custom_archive_query($query) {
    if ( is_post_type_archive('my-custom-post-type') &&  $query->is_main_query() && !is_admin() ) {
        $query->set( 'posts_per_page', 8 );
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'meta_key', 'premium');
        $query->set( 'order', 'DESC' );
    }
}
add_action('pre_get_posts', 'custom_archive_query');

It’s somewhat important to use $query->is_main_query() in the if-statement, because otherwise every other kind of query (e.g. WP menus or latest posts-widgets) won’t display anything.

tech