Classipress make sticky ads on the top for each category

Posted: December 11, 2014 in web

on functions.php


function wpb_cpt_sticky_at_top($posts) {

// apply it on the archives only
    if (is_main_query() && !is_single() && !is_admin() && !is_search()) {
        global $wp_query;

        $sticky_posts = get_option('sticky_posts');
        $num_posts = count($posts);
        $sticky_offset = 0;

        // Find the sticky posts
        for ($i = 0; $i < $num_posts; $i++) {

            // Put sticky posts at the top of the posts array
            if (in_array($posts[$i]->ID, $sticky_posts)) {
                $sticky_post = $posts[$i];

                // Remove sticky from current position
                array_splice($posts, $i, 1);

                // Move to front, after other stickies
                array_splice($posts, $sticky_offset, 0, array($sticky_post));

                // Remove post from sticky posts array
                $offset = array_search($sticky_post->ID, $sticky_posts);

        // Look for more sticky posts if needed
        if (!empty($sticky_posts)) {

            $stickies = get_posts(array(
                'post__in' => $sticky_posts,
                'post_type' => $wp_query->query_vars['post_type'],
                'post_status' => 'publish',
                'nopaging' => true

            foreach ($stickies as $sticky_post) {
                array_splice($posts, $sticky_offset, 1, array($sticky_post));

    return $posts;

add_filter('the_posts', 'wpb_cpt_sticky_at_top');

  1. Brian says:


    Do you know how I can put the classipress ad categories widget into the header next to my logo?

    I have followed tutorial after tutorial, still can’t get it

    Thank you

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s