Support » Plugin: Sticky Posts - Switch » Sticky making posts show in all categories

  • sarahpaigemorrow

    (@sarahpaigemorrow)


    I’m trying to make some sticky posts for only one category page.

    I have 5 posts that are ONLY in the category “EVENTS”, but when I make them sticky, they will show up on all category pages, for example, “NEWS”, which should only show posts in the “NEWS” category.

    Please help!

    Thanks.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Alex Pashin

    (@alexpashin)

    Hello everyone, I faced to the same issue. Please help. Many thanks!

    I installed this plugin with the hope that this is what the functionality would be.

    It would be great if the developer of the plugin could allow adding sticky posts to the top of the category the post is in rather than ALL categories.

    I am confused by this – I thought that was the whole point of this plugin. Something has definitely changed in core functionality. This functionality was easy with a different plugin in the past, but that no longer works, and neither does this. Is there an easy code fix somewhere? perhaps using is_category()?

    Summary:
    1. Posts (sticky or not) should only show in category pages if they are in that category. 2. Sticky posts with that category should be at the top.
    3. Sticky posts should not show at all on category pages they are NOT in – surely?

    I have this exact same issue, sticky posts appearing in categories that they are not a part of. No solution so far. Thanks!

    I got it to work eventually – this works for me (obviously try at your own risk etc. etc.)

    <?php
    
    /* copy commenty stuff here from existing version of plugin */
    
    class PD_StickyPostsInCategory {
        function PD_StickyPostsInCategory() {
            return $this->__construct();
        }
    
        function __construct() {
            define('PD_SPIC_VERSION', '2.1');
            //e.g. /var/www/example.com/wordpress/wp-content/plugins/exit-crusher
            define("PD_SPIC_DIR", plugin_dir_path(__FILE__));
            //e.g. exit-crusher/exit-crusher.php
            define("PD_SPIC_BASENAME", plugin_basename(__FILE__));
    
            add_filter('the_posts', array(
                $this,
                'putStickyOnTop'
            ));
            add_filter('post_class', array(
                $this,
                'addStickyClass'
            ) , 10, 3);
        }
    
        function putStickyOnTop($posts)
    
     {
            $page = 1;
            if (empty($wp_query->query_vars['nopaging']) && !$wp_query->is_singular) {
                $page = absint($wp_query->query_vars['paged']);
    
                if (!$page) {
                    $page = 1;
                }
            }
    
            // Put sticky posts at the top of the posts array
            $sticky_posts = get_option('sticky_posts');
            $current_category = get_cat_ID(single_term_title('', false));
            if ($page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$wp_query->query_vars['ignore_sticky_posts']) {
                $num_posts = count($posts);
                $sticky_offset = 0;
                // Loop over posts and relocate stickies to the front.
                for ($i = 0;$i < $num_posts;$i++) {
                    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
                        ));
                        // Increment the sticky offset. The next sticky will be placed at this offset.
                        $sticky_offset++;
                        // Remove post from sticky posts array
                        $offset = array_search($sticky_post->ID, $sticky_posts);
                        unset($sticky_posts[$offset]);
                    }
                }
    
                // If any posts have been excluded specifically, Ignore those that are sticky.
                if (!empty($sticky_posts) && !empty($q['post__not_in'])) {
                    $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
                }
    
                // Fetch sticky posts that weren't in the query results
                if (!empty($sticky_posts) && !is_user_logged_in() && is_category()) {
                    // Prevent maximum function nesting level error
                    remove_filter('the_posts', array(
                        $this,
                        'the_posts'
                    ) , 1, 2);
    
                    $stickies = get_posts(array(
                        'post__in' => $sticky_posts,
                        'post_type' => $wp_query->query_vars['post_type'],
                        'post_status' => 'publish',
    					'cat' => $current_category,
                        'nopaging' => true
                    ));
    
                    add_filter('the_posts', array(
                        $this,
                        'the_posts'
                    ) , 1, 2);
    
                    foreach ($stickies as $sticky_post) {
                        array_splice($posts, $sticky_offset, 0, array(
                            $sticky_post
                        ));
                        $sticky_offset++;
                    }
                }
            }
    
            return $posts;
    
        }
    
        function addStickyClass($classes, $class, $post_id) {
            if (is_sticky() && !isset($classes['sticky'])) {
                $classes[] = 'sticky';
            }
            return $classes;
        }
    
        function activate() {
        }
    }
    
    $PD_StickyPostsInCategory = new PD_StickyPostsInCategory();
    
    register_activation_hook(__FILE__, array(
        'PD_StickyPostsInCategory',
        'activate'
    ));
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.