# Sticky Posts First in Category Archives

> Puts sticky posts at the top of the category archives they belong to, the same way WordPress already does on the blog page.

- Type: PHP
- Runs: Run everywhere
- Category: [Archives](https://wpide.com/library/category/archive/)
- Tags: sticky, categories, archives
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/sticky-posts-first-in-categories/

## Code

```php
add_filter( 'the_posts', function ( $posts, $query ) {
    if ( is_admin() || ! $query->is_main_query() || ! $query->is_category() || $query->is_paged() ) {
        return $posts;
    }

    $sticky = array_map( 'absint', (array) get_option( 'sticky_posts', array() ) );

    if ( ! $sticky ) {
        return $posts;
    }

    $pinned = get_posts( array(
        'post__in'            => $sticky,
        'category__in'        => array( $query->get_queried_object_id() ),
        'numberposts'         => count( $sticky ),
        'ignore_sticky_posts' => true,
        'no_found_rows'       => true,
    ) );

    if ( ! $pinned ) {
        return $posts;
    }

    $pinned_ids = wp_list_pluck( $pinned, 'ID' );
    $others     = array_filter( $posts, function ( $post ) use ( $pinned_ids ) {
        return ! in_array( $post->ID, $pinned_ids, true );
    } );

    return array_merge( $pinned, array_values( $others ) );
}, 10, 2 );
```

## Notes

Only the first page of each category archive is affected.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Sticky Posts First in Category Archives" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
