Home / Archive / Show Sticky Posts on Top of the Archive Templates – WPX Tetris Theme
Duplicate Snippet

Embed Snippet on Your Site

Show Sticky Posts on Top of the Archive Templates – WPX Tetris Theme

## Sticky Posts on Archive Pages - WPX Tetris Theme

**Description:** Display sticky posts first on all archive pages (category, tag, author, date) for [WPX Tetris Theme](https://github.com/WP-Developer-Hub/wpex-tetris). Sticky posts maintain their admin-set order at the top, followed by normal posts chronologically.

**Works with all:** WPX Tetris archive • Category/Tag/Author/Date archives • No impact on single posts/home/admin if `universal_toggle_post_badge` is **on**
**Theme:** [WPX Tetris](https://github.com/WP-Developer-Hub/wpex-tetris)

Code Preview
php
<?php
add_action('pre_get_posts', function($query) {
    if (!is_admin() && $query->is_main_query() && is_archive() && get_theme_mod('universal_toggle_post_badge', 'true')) {
        $sticky_posts = get_option('sticky_posts');
        if (!empty($sticky_posts)) {
            add_filter('posts_orderby', function($orderby, $query) use ($sticky_posts) {
                global $wpdb;
                $sticky_list = implode(',', array_map('intval', $sticky_posts));
                return "FIELD({$wpdb->posts}.ID, {$sticky_list}) DESC, {$orderby}";
            }, 10, 2);
        }
    }
});

Comments

Add a Comment