Skip to main content
added 227 characters in body
Source Link
mll
  • 21
  • 4

Pieter's answer does work, but it displays all sticky posts even on pages when we want to display filtered posts (to a given category for instance).

The following works for me. Adding this to functions.php only displays stickies within the subset of filtered posts :

add_filter('the_posts', 'bump_sticky_posts_to_top');
function bump_sticky_posts_to_top($posts) {
    $stickies = array();
    foreach($posts as $i => $post) {
        if(is_sticky($post->ID)) {
            $stickies[] = $post;
            unset($posts[$i]);
        }
    }
    return array_merge($stickies, $posts);

(Credits : http://pastebin.com/Y5jVrKg4, with a slight change to prevent a Warning: array_merge() [function.array-merge]: Argument #1 is not an array on line 9 error)

However, there is a big flaw. It will not promote a sticky post on top if this sticky post in not to be present in the current page.

A system that would set the .sticky CSS class to the sticky posts would also be nice.

Pieter's answer does work, but it displays all sticky posts even on pages when we want to display filtered posts (to a given category for instance).

The following works for me. Adding this to functions.php only displays stickies within the subset of filtered posts :

add_filter('the_posts', 'bump_sticky_posts_to_top');
function bump_sticky_posts_to_top($posts) {
    $stickies = array();
    foreach($posts as $i => $post) {
        if(is_sticky($post->ID)) {
            $stickies[] = $post;
            unset($posts[$i]);
        }
    }
    return array_merge($stickies, $posts);

(Credits : http://pastebin.com/Y5jVrKg4, with a slight change to prevent a Warning: array_merge() [function.array-merge]: Argument #1 is not an array on line 9 error)

Pieter's answer does work, but it displays all sticky posts even on pages when we want to display filtered posts (to a given category for instance).

The following works for me. Adding this to functions.php only displays stickies within the subset of filtered posts :

add_filter('the_posts', 'bump_sticky_posts_to_top');
function bump_sticky_posts_to_top($posts) {
    $stickies = array();
    foreach($posts as $i => $post) {
        if(is_sticky($post->ID)) {
            $stickies[] = $post;
            unset($posts[$i]);
        }
    }
    return array_merge($stickies, $posts);

(Credits : http://pastebin.com/Y5jVrKg4, with a slight change to prevent a Warning: array_merge() [function.array-merge]: Argument #1 is not an array on line 9 error)

However, there is a big flaw. It will not promote a sticky post on top if this sticky post in not to be present in the current page.

A system that would set the .sticky CSS class to the sticky posts would also be nice.

added 25 characters in body
Source Link
mll
  • 21
  • 4

Pieter's answer does work, but it displays all sticky posts even on pages when we want to display filtered posts (to a given category for instance).

The following works for me : it. Adding this to functions.php only displays stickies within the subset of filtered posts :

add_filter('the_posts', 'bump_sticky_posts_to_top');
function bump_sticky_posts_to_top($posts) {
    $stickies = array();
    foreach($posts as $i => $post) {
        if(is_sticky($post->ID)) {
            $stickies[] = $post;
            unset($posts[$i]);
        }
    }
    return array_merge($stickies, $posts);

(Credits : http://pastebin.com/Y5jVrKg4, with a slight change to prevent a Warning: array_merge() [function.array-merge]: Argument #1 is not an array on line 9 error)

Pieter's answer does work, but it displays all sticky posts even on pages when we want to display filtered posts (to a given category for instance).

The following works for me : it only displays stickies within the subset of filtered posts :

add_filter('the_posts', 'bump_sticky_posts_to_top');
function bump_sticky_posts_to_top($posts) {
    $stickies = array();
    foreach($posts as $i => $post) {
        if(is_sticky($post->ID)) {
            $stickies[] = $post;
            unset($posts[$i]);
        }
    }
    return array_merge($stickies, $posts);

(Credits : http://pastebin.com/Y5jVrKg4, with a slight change to prevent a Warning: array_merge() [function.array-merge]: Argument #1 is not an array on line 9 error)

Pieter's answer does work, but it displays all sticky posts even on pages when we want to display filtered posts (to a given category for instance).

The following works for me. Adding this to functions.php only displays stickies within the subset of filtered posts :

add_filter('the_posts', 'bump_sticky_posts_to_top');
function bump_sticky_posts_to_top($posts) {
    $stickies = array();
    foreach($posts as $i => $post) {
        if(is_sticky($post->ID)) {
            $stickies[] = $post;
            unset($posts[$i]);
        }
    }
    return array_merge($stickies, $posts);

(Credits : http://pastebin.com/Y5jVrKg4, with a slight change to prevent a Warning: array_merge() [function.array-merge]: Argument #1 is not an array on line 9 error)

Source Link
mll
  • 21
  • 4

Pieter's answer does work, but it displays all sticky posts even on pages when we want to display filtered posts (to a given category for instance).

The following works for me : it only displays stickies within the subset of filtered posts :

add_filter('the_posts', 'bump_sticky_posts_to_top');
function bump_sticky_posts_to_top($posts) {
    $stickies = array();
    foreach($posts as $i => $post) {
        if(is_sticky($post->ID)) {
            $stickies[] = $post;
            unset($posts[$i]);
        }
    }
    return array_merge($stickies, $posts);

(Credits : http://pastebin.com/Y5jVrKg4, with a slight change to prevent a Warning: array_merge() [function.array-merge]: Argument #1 is not an array on line 9 error)